Mengunggah data JSON ke S3 Bucket di Node JS
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
var s3 = new AWS.S3();
var obj = {
firstname: "Navjot",
lastname: "Dhanawat"
};
var buf = Buffer.from(JSON.stringify(obj));
var data = {
Bucket: 'bucket-name',
Key: 'filename.json',
Body: buf,
ContentEncoding: 'base64',
ContentType: 'application/json',
ACL: 'public-read'
};
s3.upload(data, function (err, data) {
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded!!!');
}
});
// IMPORTANT !!!!
// real node js route for POST request
// S3 Upload Test Starts
// const express = require('express');
var AWS = require('aws-sdk');
// for s3 data from create-project
var data;
var buf;
var s3
AWS.config.update({
accessKeyId: "AKIA2IHT2A4BSEINFZWC",
secretAccessKey: "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",
region: 'us-east-2'
});
s3 = new AWS.S3();
// Define POST route
router.post('/test-upload', (request, response) => {
buf = Buffer.from(JSON.stringify(request.body));
data = {
ACL: 'public-read',
Bucket: "chunkupload",
Key: `uploadtest/${Date.now().toString()}`,
Body: buf,
ContentEncoding: 'base64',
ContentType: 'application/json',
};
s3.upload(data, function (err, data) {
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded!!!');
response.json({"upload":"successful"});
}
});
});
// S3 Upload Test Ends
Light Lark