How to update ACL for all S3 objects in a folder with AWS CLI?
Asked Answered
C

3

6

As part of an automated process in CodeBuild I want to update Access Control List for all files in a given folder (or more specifically all objects with given prefix). How to do it in a single line of bash code?

Cardinale answered 11/12, 2018 at 14:53 Comment(0)
C
16

The following one liner works perfectly

aws s3api list-objects --bucket $BUCKET_NAME$ --prefix $FOLDER_NAME$ --query "(Contents)[].[Key]" --output text | while read line ; do aws s3api put-object-acl --acl public-read --bucket $BUCKET_NAME$ --key $line ; done

it's not formatted as code, so that it's readable without scrolling!

Cardinale answered 11/12, 2018 at 14:53 Comment(1)
this requires me to press q each time a $line is used as it prints the (END) as outputUndertaking
U
2

You can use aws s3 cp

aws s3 cp --grants foo=bar=baz s3://mybucket/mydir s3://mybucket/mydir

Reference https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html

Uralian answered 21/6, 2019 at 6:36 Comment(0)
D
0

list all objects and modify acl by put-object-acl

acl=public-read

aws s3 ls s3://$bucket --recursive --endpoint-url=$endpoint | awk '{print $4}' > tos-objects.txt

cat tos-objects.txt | while read object
do
    echo -e "set acl of \033[31m $object \033[0m as $acl"
    aws s3api put-object-acl --bucket $bucket --key $object --acl $acl --endpoint-url=$endpoint
done
Dudleyduds answered 27/12, 2021 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.