【发布时间】:2017-08-27 18:32:24
【问题描述】:
我在区域 BUCKET_REGION 上有一个名为 BUCKET 的 S3 存储桶。我正在尝试允许我的网络和移动应用程序的用户将图像文件上传到这些存储桶,前提是他们满足基于 Content-Type 和 Content-Length 的某些限制(即,我只想允许小于 3mbs 的 jpeg上传)。上传后,文件应该可以公开访问。
基于对 AWS 文档的大量挖掘,我假设该过程在我的前端应用程序上应该如下所示:
const a = await axios.post('my-api.com/get_s3_id');
const b = await axios.put(`https://{BUCKET}.amazonaws.com/{a.id}`, {
// ??
headersForAuth: a.headersFromAuth,
file: myFileFromSomewhere // i.e. HTML5 File() object
});
// now can do things like <img src={`https://{BUCKET}.amazonaws.com/{a.id}`} />
// UNLESS the file is over 3mb or not an image/jpeg, in which case I want it to be throwing errors
在我的后端 API 上我会做类似的事情
import aws from 'aws-sdk';
import uuid from 'uuid';
app.post('/get_s3_id', (req, res, next) => {
// do some validation of request (i.e. checking user Ids)
const s3 = new aws.S3({region: BUCKET_REGION});
const id = uuid.v4();
// TODO do something with s3 to make it possible for anyone to upload pictures under 3mbs that have the s3 key === id
res.json({id, additionalAWSHeaders});
});
我不确定我应该查看哪些确切的 S3 方法。
以下是一些不起作用的事情:
我看到很多提到(一个非常古老的)API 可通过
s3.getSignedUrl('putObject', ...)访问。但是,这似乎不再支持可靠地设置ContentLength——至少不再支持。 (见https://stackoverflow.com/a/28699269/251162。)我还看到了一个使用
HTTP POST和form-dataAPI 的更接近工作的示例,该API 也很旧。我想如果没有其他选择,这可能会完成,但我担心它不再是“正确”的做事方式——此外,它似乎做了很多手动加密等而不使用官方节点开发工具包。 (见https://stackoverflow.com/a/28638155/251162。)
【问题讨论】:
-
您能否确保您的 S3 存储桶附加了正确的策略。它需要公开。
标签: javascript node.js amazon-web-services express amazon-s3