S3: make a public folder private again?

前端 未结 12 2048
北恋
北恋 2021-01-30 20:16

How do you make an AWS S3 public folder private again?

I was testing out some staging data, so I made the entire folder public within a bucket. I\'d like to restrict it

12条回答
  •  爱一瞬间的悲伤
    2021-01-30 20:43

    There are two ways to manage this:

    1. Block all the bucket (simplier but does not applies to all use cases like a s3 bucket with static website and a sub folder for CDN) - https://aws.amazon.com/blogs/aws/amazon-s3-block-public-access-another-layer-of-protection-for-your-accounts-and-buckets/
    2. Block access to a directory from the s3 bucket that was granted Make Public option where you can execute the script from ascobol (I just rewrite it with boto3)
    #!/usr/bin/env python
    #remove public read right for all keys within a directory
    
    #usage: remove_public.py bucketName folderName
    
    import sys
    import boto3
    
    BUCKET = sys.argv[1]
    PATH = sys.argv[2]
    s3client = boto3.client("s3")
    paginator = s3client.get_paginator('list_objects_v2')
    page_iterator = paginator.paginate(Bucket=BUCKET, Prefix=PATH)
    for page in page_iterator:
        keys = page['Contents']
        for k in keys:
            response = s3client.put_object_acl(
                            ACL='private',
                            Bucket=BUCKET,
                            Key=k['Key']
                        )
    

    cheers

提交回复
热议问题