从 url 下载文件并将其上传到 AWS S3 而不保存 - node.js [英] Download file from url and upload it to AWS S3 without saving - node.js
本文介绍了从 url 下载文件并将其上传到 AWS S3 而不保存 - node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个应用程序,它从 url 下载图像,然后使用 aws-sdk 将其上传到 S3 存储桶.
I'm writing an application which downloads images from a url and then uploads it to an S3 bucket using the aws-sdk.
以前我只是像这样下载图像并将它们保存到磁盘.
Perviously I was just downloading images and saving them to disk like this.
request.head(url, function(err, res, body){
request(url).pipe(fs.createWriteStream(image_path));
});
然后像这样将图像上传到 AWS S3
And then uploading the images to AWS S3 like this
fs.readFile(image_path, function(err, data){
s3.client.putObject({
Bucket: 'myBucket',
Key: image_path,
Body: data
ACL:'public-read'
}, function(err, resp) {
if(err){
console.log("error in s3 put object cb");
} else {
console.log(resp);
console.log("successfully added image to s3");
}
});
});
但我想跳过将图像保存到磁盘的部分.有什么方法可以将来自 request(url)
的响应 pipe
传输到变量然后上传?
But I would like to skip the part where I save the image to disk. Is there some way I can pipe
the response from request(url)
to a variable and then upload that?
推荐答案
这里有一些 javascript 可以很好地做到这一点:
Here's some javascript that does this nicely:
var options = {
uri: uri,
encoding: null
};
request(options, function(error, response, body) {
if (error || response.statusCode !== 200) {
console.log("failed to get image");
console.log(error);
} else {
s3.putObject({
Body: body,
Key: path,
Bucket: 'bucket_name'
}, function(error, data) {
if (error) {
console.log("error downloading image to s3");
} else {
console.log("success uploading to s3");
}
});
}
});
这篇关于从 url 下载文件并将其上传到 AWS S3 而不保存 - node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文