簡體   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