Reputation: 99
How to create a new S3 bucket in AWS using nodejs?
I need to upload a large number of the image file on the s3 bucket for ease of using and manage the storage space on the cloud instead of my local server storage.
Upvotes: 4
Views: 6268
Reputation: 2237
Check the document on https://docs.amazonaws.cn/en_us/sdk-for-javascript/v2/developer-guide/s3-example-creating-buckets.html
var s3bucket = new AWS.S3({params: {Bucket: 'test_bucket/sub_bucket'}});
will create an extra file. Take out the params in the parentheses. I found out that Amazon's quick start guide example creates an extra file. This way is the correct way to do it.
// Create a bucket using bound parameters and put something in it.
var s3bucket = new AWS.S3();
s3bucket.createBucket(function() {
var params = {Bucket: 'bucket/sub-bucket', Key: 'file_name1', Body: 'Hello!'};
s3bucket.putObject(params, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
res.writeHead(200, {'Content-Type':'text/plain'});
res.write("Successfully uploaded data to bucket/sub-bucket/");
res.end()
}
});
});
Upvotes: 1
Reputation: 1799
Use S3 module on your node server and read the documentation.
var s3 = require('s3');
var client = s3.createClient({
maxAsyncS3: 20, // this is the default
s3RetryCount: 3, // this is the default
s3RetryDelay: 1000, // this is the default
multipartUploadThreshold: 20971520, // this is the default (20 MB)
multipartUploadSize: 15728640, // this is the default (15 MB)
s3Options: {
accessKeyId: "your s3 key",
secretAccessKey: "your s3 secret",
// any other options are passed to new AWS.S3()
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
},
});
Upvotes: 0
Reputation: 4533
Try this
var params = {
Bucket: "examplebucket",
CreateBucketConfiguration: {
LocationConstraint: "eu-west-1"
}
};
s3.createBucket(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
For more info read this link
Upvotes: 2