node.js - 通过应用授予对 s3 私有存储桶中用户特定图像的访问权限
问题描述
我有一个 MEAN 堆栈应用程序,用户可以在其中上传图像。目前,我将图像的 URL 存储在 mongodb 中,并将图像本身存储在指定文件夹的文件系统中。我读过最好将这些图像存储在 AWS 或 firebase 之类的地方以实现可扩展性,因此我决定使用 AWS。
我创建了一个存储桶,可以通过 multer 上传图片。
如何允许用户仅在登录我的应用程序时检索他们的图像?前端和后端的身份验证已经完成,但我不知道如何使用它来访问 s3 存储桶并且只获取用户特定的图像。
我可以公开存储桶并轻松构建 URL,但这意味着每个人(无论他们是否登录到我的应用程序)都可以访问它们,这不是我想要的。
解决方案
If you can authenticate your users as Amazon Cognito users then during a login they will assume a role.
If that role has a policy attached for S3 you can use the ${cognito-identity.amazonaws.com:sub}
variable. By structuring your S3 bucket to use key paths containing this variable in it you can develop a policy that will only grant access to keys with the prefix of that user id.
Take a look here for an example S3 policy.
推荐阅读
- python - Python:使用预先计算的元素加速大双和
- laravel - 如何发送带有“假期订单”确认的电子邮件?
- android - FirebaseAuth.getInstance().getCurrentUser() 返回已删除的用户
- laravel - 如何在 laravel 5 中使用 ckeditor
- sql - SQL RegEx:如何从此字符串中提取日期
- javascript - Vue.js JSON 分组数组多次
- javascript - 如何根据香草javascript中的属性对对象数组进行分组
- machine-learning - SageMaker Estimator.fit() 没有将“训练”输入传递给训练实例
- python - 想要反转我的字符串位置,特殊字符应该在那里
- jquery - jQuery悬停触发“鼠标离开”而不离开光标