小编Dak*_*nch的帖子

如果不存在则创建 S3 存储桶

我的 node.js 应用程序需要将文件上传到 S3。大多数情况下,它会将文件上传到现有存储桶。但有时,需要先创建存储桶。有没有办法检查bucket是否已经存在,如果没有,在开始上传之前创建它?这是我到目前为止所得到的:

function uploadit () {
  'use strict';
  console.log('Preparing to upload the verse.')
  var s3 = new AWS.S3({apiVersion: '2006-03-01'});
  var uploadParams = {Bucket: 'DynamicBucketName', key: '/test.mp3', Body: myvalue, ACL: 'public-read'};
  var file = 'MyVerse.mp3';

  var fs = require('fs');
  var fileStream = fs.createReadStream(file);
  fileStream.on('error', function(err) {
    console.log('File Error', err);
  });
  uploadParams.Body = fileStream;

  var path = require('path');
  uploadParams.Key = book.replace(/ /g, "")+reference.replace(/ /g, "")+"_user"+userid+".mp3";

  // call S3 to retrieve upload file to specified bucket
  s3.upload (uploadParams, function (err, data) …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 node.js

2
推荐指数
1
解决办法
3362
查看次数

标签 统计

amazon-s3 ×1

node.js ×1