aws s3api create-bucket --bucket mybucket --create-bucket-configuration LocationConstraint=ap-northeast-1
# バケット一覧 aws s3 ls s3://mybucket # s3バケットを再帰的にローカルにコピー aws s3 cp s3://mybucket/myfolder myfolder --recursive # s3バケットとローカルを同期 aws s3 sync s3://mybucket/myfolder myfolder --exclude *.tmp --exact-timestamps # デフォルトに指定したリージョン以外の操作には --region オプションを付ける aws s3 cp s3://mybucket/myfolder myfolder --recursive --region ap-northeast-1
aws s3 rm s3://mybucket/ --recursive
aws s3api get-bucket-lifecycle --bucket mybucket > bucket-lifecycle.30.json cat bucket-lifecycle.30.json { "Rules": [ { "Status": "Enabled", "Prefix": "", "Expiration": { "Days": 30 }, "ID": "lifecycle-30" } ] } aws s3api put-bucket-lifecycle --bucket mybucket --lifecycle-configuration file://bucket-lifecycle.30.json
s3://example /dir1 /dummy1.txt /dummy1.txt
aws s3 sync s3://example/dir1/ s3://example/ --profile example --exact-timestamps
要望は上がっている
# TZ=JSTの場合 aws s3 ls s3://my-bucket/ --profile example 2021-08-25 12:05:23 5 example1.html 2021-03-04 17:35:57 38 index.html # TZ=UTCの場合 TZ=UTC aws s3 ls s3://my-bucket/ --profile example 2021-08-25 03:05:23 5 example1.html 2021-03-04 08:35:57 38 index.html
aws s3api list-objects --query 'Contents[].[Key, LastModified]' --bucket my-bucket --profile example --output text example1.html 2021-08-25T03:05:23+00:00 index.html 2021-03-04T08:35:57+00:00
aws s3 ls --profile example --region us-east-1 | grep my-old-bucket 2014-10-24 16:06:51 my-old-bucket aws s3 ls --profile example --region ap-northeast-1 | grep my-old-bucket 2020-06-20 03:46:31 my-old-bucket
aws s3api list-objects-v2 --bucket <bucket> --query "Contents[?contains(Key, '<key name>')]" --profile <profile>
aws s3 ls s3://example/ --output text | awk '{print $4;}'
aws s3api list-objects-v2 \ --bucket example \ --prefix prefix/ \ --query 'Contents[].[Key]' \ --output text
aws_profile=example region=ap-northeast-1 bucket_name=example-bucket start_time="$(date +%Y-%m-%d -d '-1 day')T00:00:00Z" end_time="$(date +%Y-%m-%d -d '-1 day')T23:59:59Z" period=86400 bucket_size_info=$(aws cloudwatch get-metric-statistics \ --profile ${aws_profile} \ --region ${region} \ --namespace AWS/S3 \ --metric-name BucketSizeBytes \ --dimensions Name=BucketName,Value=${bucket_name} Name=StorageType,Value=StandardStorage \ --statistics Sum \ --start-time ${start_time} \ --end-time ${end_time} \ --period ${period}) bucket_size=$(echo ${bucket_size_info} | jq -r '.Datapoints[].Sum') bucket_size_gb=$(echo "scale=2; ${bucket_size} / 1024 / 1024 / 1024" | bc)
bucket_objects=$(aws cloudwatch get-metric-statistics \ --profile ${aws_profile} \ --region ${region} \ --namespace AWS/S3 \ --metric-name NumberOfObjects \ --dimensions Name=BucketName,Value=${bucket_name} Name=StorageType,Value=AllStorageTypes \ --statistics Average \ --start-time ${start_time} \ --end-time ${end_time} \ --period ${period}) bucket_objects_count=$(echo ${bucket_objects} | jq -r '.Datapoints[].Average')
aws s3 ls s3://my-bucket/ --recursive --human --sum --profile example ... Total Objects: 14 Total Size: 56.5 KiB
aws s3 presign s3://mybucket/example.txt --expires-in 3600
profile=default s3_path=s3://example-bucket/example1.gz expires_in=$((8*3600)) aws sts assume-role \ --role-arn "arn:aws:iam::012345678901:role/assumerole-example" \ --role-session-name "session1" \ --duration-seconds $expires_in \ --profile account1 > session1.json export AWS_REGION=ap-northeast-1 export AWS_ACCESS_KEY_ID=$(cat session1.json | jq -r '.Credentials.AccessKeyId') export AWS_SECRET_ACCESS_KEY=$(cat session1.json | jq -r '.Credentials.SecretAccessKey') export AWS_SESSION_TOKEN=$(cat session1.json | jq -r '.Credentials.SessionToken') aws s3 presign $s3_path --expires-in $expires_in
unset $(env | grep -o -P '^AWS_[\w\d]+')
aws s3 cp s3://mybucket/logs/*.log ./
aws s3 cp s3://mybucket/ ./ --recursive --exclude "*" --include "*.log"
mybucket |-test01 |-test02 |-test03
aws s3 sync s3://mybucket/ ./ --exclude "*" --include "test01/*" --include "test02/*" --exact-timestamps
AWS_PROFILE=default for bucket in $(aws --profile $AWS_PROFILE s3api list-buckets --query "Buckets[].[Name]" --output text); do \ region=$(aws --profile $AWS_PROFILE s3api get-bucket-location --bucket $bucket --output text); \ echo "---- $bucket"; \ aws --profile $AWS_PROFILE --region $region s3api get-bucket-policy --bucket $bucket; \ done > $AWS_PROFILE.s3.bucket-policy.json