S3 †
- バケットライフサイクルの指定。JSONが複雑なのでManagement Consoleで設定して、その値を取得すると楽
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
aws s3 lsでバケット作成日がregion指定で異なる値を返す †
- 環境
- aws-cli/2.1.0 Python/3.7.3 Linux/4.19.128-microsoft-standard exe/x86_64.ubuntu.18
- aws-cli/1.18.176 Python/3.6.8 Linux/4.19.128-microsoft-standard botocore/1.19.16
keyの部分一致したobjectを抽出 †
aws s3api list-objects-v2 --bucket <bucket> --query "Contents[?contains(Key, '<key name>')]" --profile <profile>
- 前方一致で良い場合は「--prefix <key name>」が使える
ls: key名だけの取得 †
object数と合計ファイルサイズ †
- awscli cloudwatchから取得。速い
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')
署名付オブジェクトURLの有効期限 †
- awscliのpresignはダウンロードのみ対応。
- boto3の s3.generate_presigned_url()でput可能なpresign urlを払い出せるようだ。
- 使用したアクセスキーの有効期限により、指定した有効期限より短い時間で切れる事がある。
- IAM user: max 7 days
- IAM instance profile: max 6 hour
- STS: max 36 hour
署名付きオブジェクトURLの生成 †
S3 cp: ワイルドカードを使う †
S3 sync †
aws s3 sync s3://mybucket/ ./ --exclude "*" --include "test01/*" --include "test02/*"
S3のbucket policy取得 †
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