繁体   English   中英

如何允许用户删除 S3 存储桶中的对象?

[英]How to allow users to delete objects in S3 bucket?

我有一个移动应用程序,它让用户通过 AWS Cognito 进行身份验证,他们最终进入用户池。 他们能够将对象放入存储桶没问题,但他们不能删除。 我想要做的是让每个登录用户都能够删除存储桶中的文件。

文件路径例如: my_bucket_name/protected/eu-west-2:de55c2rf-8f1e-836d-88f9-82da662aau6dt/videos/video1

要删除,我称之为:

import { Storage } from 'aws-amplify'; 

. . .

delFromS3 = async () => {
      Storage.remove('protected/eu-west-2:de55c2rf-8f1e-836d-88f9-82da662aau6dt/videos/video1')  
         .then(result => console.log('Deleted Video from S3'))
         .catch(err => console.log('Deleting video from S3 error: ', err));
  }

调用此方法时,我一直收到错误访问被拒绝,因此我添加了一个存储桶策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "cognito-identity.amazonaws.com"
            },
            "Action": "s3:DeleteObject",
            "Resource": "arn:aws:s3:::my_bucket_name/*"
        }
    ]
}

此函数唯一返回结果的时间是当我输入"Principal": "*"但这会使我的存储桶向任何人公开,这是我不想做的。 它也不接受这是一个有效的主要政策:

"Principal": { "AWS": [ "arn:aws:cognito-idp:eu-west-2:968257789397:userpool/eu-west-2_2ecGAT74q" ] }. 

所以我需要知道正确的校长是什么。

所以我要么需要一种方法来允许认知用户池中的用户被授权删除一个对象。 或者因为我手动知道每个用户子存储桶中文件的路径(例如/protected/eu-west-2:de55c2rf-8f1e-836d-88f9-82da662aau6dt/videos/video1 )只需在我的delFromS3()函数中传递它. 我的存储桶策略应该读什么? 还有什么我在这里想念的吗?

请帮忙!

我会为 DELETE 等操作创建一个 IAM 用户,并仅将您的认知 ID 用于访问。 对于 IAM 用户,您的 Principal 将如下所示:

"Principal": {
    "AWS": "arn:aws:iam::841367581918:user/your-iam-name"
},

您将在 api 文件的顶部为该用户提供 accessKey,如下所示:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
s3.config.update({
    region: process.env.BUCKET_REGION,
    accessKeyId: process.env.IAM_ACCESS_KEY,
    secretAccessKey: process.env.IAM_SECRET_KEY
});

然后,要删除您的对象:

deleteObject: async (req, res) => {

    const bucket = process.env.YOUR_BUCKET;

    try {
        // delete record in DB
        //....
        let cognitoId = req.user.cognitoId;
        let key = cognitoId + '/' + path; // my folder name is user's cognitoId and path is the rest of url.

        const params = {
            Bucket: bucket,
            Key: key
        }

        try {
            await s3.headObject(params).promise();
            console.log("File found");
            try {
                await s3.deleteObject(params).promise();
                console.log("deleted successfully");
            }
            catch (error) {
                console.log(error);
                res.status(500).send(error.message);
            }
        }
        catch (error) {
            console.log(error);
            res.status(500).send(error.message);
        }
        res.status(200).send("success");
    }
    catch (error) {
        console.log(error);
        res.status(500).send(error.message);
    }
}

Amplify CLI 将帮助您针对“public”、“protected”和“private”文件夹配置正确的 S3 存储桶策略。 https://aws-amplify.github.io/docs/ios/storage#restrict-access要完成您想做的事情,您可以

  1. 使用 Amplify.Storage,使用accessLevel: .protected上传,这会将对象上传到<bucket>/protected/<key>下的 S3

  2. 使用相同的访问级别下载以在<bucket>/protected/<key>处检索

let options = StorageDownloadDataRequest.Options(accessLevel: .protected)
Amplify.Storage.downloadData(key: "myKey", options: options) { (event) in
    ...
}

从本质上讲,Amplify Storage 插件为您在密钥前面预先设置了存储访问级别。 如果在调用中未指定 accessLevel,则默认情况下将添加“public”。

您可以在此处找到相关的博客文章: https : //aws.amazon.com/blogs/mobile/introducing-aws-amplify-for-ios-and-android/

对应的示例应用: https : //github.com/nikhil-dabhade/amplify-ios-samples/tree/master/samples/ios/amplify/storage/AmplifyStorageSampleApp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM