简体   繁体   中英

How to list files which has certain tag in S3 bucket?

Here is my code:

import json, boto3
def lambda_handler(event, context):
    client = boto3.client("s3", region_name='us-east-1')
    response = client.list_objects(Bucket='test-bucket')
    return response

Above code is working fine for me. But I want to list all objects in the bucket which has a particular tag on the file(object), let's say

department:finance

I write below policy for it:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:List*",
            "Resource": "arn:aws:s3:::mynewbucket1122",
            "Condition": {
                "StringEquals": {
                    "s3:ExistingObjectTag/department": "finance"
                }
            }
        }
    ]
}

But I am getting errors, how can I get expected result.

The bucket policy, even if it was correct, does not filter your objects by the tag. You still have to call your list_objects , get the full list of objects, and iteratively , inspect their tags to get only those that you are interested in.

In other words, you have to do the filtering in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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