首页 > 解决方案 > 通过应用授予对 s3 私有存储桶中用户特定图像的访问权限

问题描述

我有一个 MEAN 堆栈应用程序,用户可以在其中上传图像。目前,我将图像的 URL 存储在 mongodb 中,并将图像本身存储在指定文件夹的文件系统中。我读过最好将这些图像存储在 AWS 或 firebase 之类的地方以实现可扩展性,因此我决定使用 AWS。

我创建了一个存储桶,可以通过 multer 上传图片。

如何允许用户仅在登录我的应用程序时检索他们的图像?前端和后端的身份验证已经完成,但我不知道如何使用它来访问 s3 存储桶并且只获取用户特定的图像。

我可以公开存储桶并轻松构建 URL,但这意味着每个人(无论他们是否登录到我的应用程序)都可以访问它们,这不是我想要的。

标签: node.jsmongodbamazon-web-servicesamazon-s3mean-stack

解决方案


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.


推荐阅读