NodeJS如何从aws s3存储桶中将文件下载到磁盘?

NodeJS如何从aws s3存储桶中将文件下载到磁盘?,第1张

NodeJS如何从aws s3存储桶中将文件下载到磁盘

这是在最新版本的aws-sdk上使用流传输的完整代码

var express = require('express');var app = express();var fs = require('fs');app.get('/', function(req, res, next){    res.send('You did not say the magic word');});app.get('/s3Proxy', function(req, res, next){    // download the file via aws s3 here    var fileKey = req.query['fileKey'];    console.log('Trying to download file', fileKey);    var AWS = require('aws-sdk');    AWS.config.update(      {        accessKeyId: "....",        secretAccessKey: "...",        region: 'ap-southeast-1'      }    );    var s3 = new AWS.S3();    var options = {        Bucket    : '/bucket-url',        Key    : fileKey,    };    res.attachment(fileKey);    var fileStream = s3.getObject(options).createReadStream();    fileStream.pipe(res);});var server = app.listen(3000, function () {    var host = server.address().address;    var port = server.address().port;    console.log('S3 Proxy app listening at http://%s:%s', host, port);});


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存