我有一个node.js REST API,它可以保存用户的“帖子”,然后以JSON API格式检索数据,这很棒,但我还需要向每个“帖子”添加一些图像。我如何从Flutter客户端直接上传到亚马逊S3,问题是我对如何处理有点困惑,因为我不想因为负载而将图像推送到服务器上。我看到了一些关于如何上传图片的插件,但问题是我需要将图片链接保存在一个数组中,以便在用户打开它时加载图片,但如果我首先需要上传图片并返回链接,我如何才能将链接保存到我的API中。谢谢!
发布于 2019-12-14 17:55:50
其思想是,您的nodejs代码必须为每个文件生成预签名的url,并将其作为json的一部分发送。
预签名URL是在不公开S3存储桶的情况下从S3访问文件的最安全的方法。
下面是获取URL的nodejs代码示例,
const getPresignedUrl = async (fileName, fileType) => {
const S3_BUCKET = 'my-s3-bucket'
const s3 = new AWS.S3(); // Create a new instance of S3
// Set up the payload of what we are sending to the S3 api
const s3Params = {
Bucket: S3_BUCKET,
Key: fileName,
Expires: 5000,
//ContentType: fileType,
// ACL: 'public-read',
ContentType: 'application/octet-stream'
};
// Make a request to the S3 API to get a signed URL which we can use to upload our file
try {
const data = await s3.getSignedUrlPromise('getObject', s3Params);
const returnData = {
signedRequest: data,
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
return returnData;
} catch (err) {
console.log('err: ', err);
}
}希望这能有所帮助。
https://stackoverflow.com/questions/59333704
复制相似问题