上载后从AWS S3获取上载的文件

时间:2019-11-01 17:33:05

标签: javascript node.js amazon-web-services amazon-s3

考虑代码:

    var express = require('express'),
        aws = require('aws-sdk'),
        bodyParser = require('body-parser'),
        multer = require('multer'),
        multerS3 = require('multer-s3');

    aws.config.update({
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        accessKeyId: 'XXXXXXXXXXXXXXX',
        region: 'us-east-1'
    });

    var app = express(),
        s3 = new aws.S3();

    app.use(bodyParser.json());

    var upload = multer({
        storage: multerS3({
            s3: s3,
            bucket: 'bucket-name',
            key: function (req, file, cb) {
                console.log(file);
                cb(null, file.originalname); //use Date.now() for unique file keys
            }
        })
    });

    //open in browser to see upload form
    app.get('/', function (req, res) {
        res.sendFile(__dirname + '/index.html');//index.html is inside node-cheat
    });

    //use by upload form
    app.post('/upload', upload.array('upl',1), function (req, res, next) {
        res.send("Uploaded!");
    });

    app.listen(3000, function () {
        console.log('Example app listening on port 3000!');
    });

如何获得用户在节点 之后成功上传到存储桶中的文件?

文件上传后,我需要对文件进行一些操作。

1 个答案:

答案 0 :(得分:1)

成功上传文件后,它将返回一个URL。使用URL,您可以检索已上传的文件。然后,您可以使用nodejs函数fs.readFileSync(file path)读取文件。