gpt4 book ai didi

node.js - 将本地文件夹中的图像上传到S3

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:27 26 4
gpt4 key购买 nike

在我的应用程序中,我将图像上传到本地/tmp 文件夹并进行一些转换。图片已正确保存在那里。之后我想将这些图像上传到 S3 存储桶,但到目前为止我只能生成空白图片。

这是我的代码:

//Pick the local image and make it binary
var fs = require('fs');
var bufferedData = '';
fs.readFile(imagePath, function (err, data) {
if (err) { throw err; }
bufferedData = new Buffer(data, 'binary');
}


//Send data to s3
const uploadToS3 = async (idKey: string, modifiers: string, bufferedData) => {
try {
return await S3.upload({
Bucket: 'mirage-thumbnails',
Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
Body: bufferedData,
ContentType: 'image/png',
ACL: 'public-read',
CacheControl: 'max-age=0',
}).promise();
} catch (e) {
console.error(e.message, e);
}
};

最佳答案

readFile 是异步的,需要等到完成后才能上传到 S3。但您可以提供 readable stream 而不是使用 readFiles3.upload,这将允许您上传大文件而不会耗尽内存,并使代码更容易一些。

S3.upload({
Bucket: 'mirage-thumbnails',
Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
Body: fs.createReadStream(imagePath),
ContentType: 'image/png',
ACL: 'public-read',
CacheControl: 'max-age=0',
}).promise();
<小时/>

在您的代码中,调用 uploadToS3 时,bufferedData 未填充。您应该等到文件被读取,然后调用uploadToS3。代码应如下所示:

const fs = require('fs');
const promisify = require('util').promisify;

// Promisify readFile, to make code cleaner and easier.
const readFile = promisify(fs.readFile);

const uploadToS3 = async(idKey, modifiers, data) => {
return S3.upload({
Bucket: 'mirage-thumbnails',
Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
Body: data,
ContentType: 'image/png',
ACL: 'public-read',
CacheControl: 'max-age=0',
}).promise();
};

const uploadImage = async(path) => {
const data = await readFile(imagePath);
// Wait until the file is read
return uploadToS3('key', 'modifier', data);
};

uploadImage('./some/path/image.png')
.then(() => console.log('uploaded!'))
.catch(err => console.error(err));
<小时/>

使用流,只需将 uploadImage 更改为:

const uploadImage = async(path) => {
const stream = fs.createReadStream(path);
return uploadToS3('key', 'modifier', stream);
};

关于node.js - 将本地文件夹中的图像上传到S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50574303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com