如何获取公共URL响应

如何获取公共URL响应

本文介绍了节点JS AWS S3文件上传。如何获取公共URL响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm uploading a file to Amazon S3 using the Node SDK.

文件上传功能正常,但我想获取文件的公共URL发回客户端。

The file uploads are working fine, but I want to get the public url of the file to send back to the client.

目前我得到的回应是:

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

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

Here is my code. I'm using a Node Express server, and Multer to handle uploads.

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


推荐答案

使用 而不是 putObject 即可。
它会在位置

这篇关于节点JS AWS S3文件上传。如何获取公共URL响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:10