37

我正在使用 Node SDK 将文件上传到 Amazon S3。

文件上传工作正常,但我想获取文件的公共 url 以发送回客户端。

目前我得到的回应是:

Successfully uploaded data { ETag: '"957cd1a335adf5b4000a5101ec1f52bf"' }

这是我的代码。我正在使用 Node Express 服务器和 Multer 来处理上传。

app.use(multer({ // https://github.com/expressjs/multer
      dest: './public/uploads/', 
      limits : { fileSize:100000 },
      rename: function (fieldname, filename) {
        var time = new Date().getTime();
        return filename.replace(/\W+/g, '-').toLowerCase() + time;
      },
      onFileUploadData: function (file, data, req, res) {
        // file : { fieldname, originalname, name, encoding, mimetype, path, extension, size, truncated, buffer }
        var params = {
          Bucket: creds.awsBucket,
          Key: file.name,
          Body: data,
          ACL: 'public-read'
        };

        var s3 = new aws.S3()
        s3.putObject(params, function (perr, pres) {
          if (perr) {
            console.log("Error uploading data: ", perr);
          } else {
            console.log("Successfully uploaded data", pres);
          }
        });
      }
    }));

    app.post('/upload-image', function(req, res){
        if(req.files.file === undefined){
            res.send("error, no file chosen");
        }
    });
4

1 回答 1

47

使用upload而不是putObject。它将在位置键中返回公共 url

于 2015-07-13T21:38:05.927 回答