简体   繁体   English

如何使用 nodejs 在 AWS 中创建新的 S3 存储桶?

[英]How to create new S3 bucket in AWS using nodejs?

How to create a new S3 bucket in AWS using nodejs?如何使用 nodejs 在 AWS 中创建新的 S3 存储桶?

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.我需要在s3存储桶上上传大量图像文件,以便于使用和管理云上的存储空间,而不是我的本地服务器存储。

  • I am working on IoT project which captures the number of images once motion detects any object in their range.我正在从事物联网项目,一旦运动检测到其范围内的任何对象,该项目就会捕获图像的数量。 So send me the step to follow the configuration of AWS s3 Bucket integration using nodejs.因此,请向我发送使用 nodejs 执行 AWS s3 Bucket 集成配置的步骤。

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有关更多信息,请阅读此链接

Check the document on https://docs.amazonaws.cn/en_us/sdk-for-javascript/v2/developer-guide/s3-example-creating-buckets.html检查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()
        }
    });
});

Use S3 module on your node server and read the documentation.在您的节点服务器上使用S3模块并阅读文档。

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
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM