https://dev.classmethod.jp/articles/manage-s3-lifecycle-via-cli/
https://dev.classmethod.jp/articles/lim-s3-lifecycle-rules/
次の 2 種類のアクションがあります。
移行アクション
失効アクション
オブジェクトは、キープレフィックス、オブジェクトタグ、またはそれらの組み合わせによってフィルタリングできます。
-- 作業概要
01/test01.txt
/test02.txt
02/test03.txt <== tag1付与
/test04.txt
ルール1: 1日後、01フォルダ配下のオブジェクトをS3 標準ストレージクラス → S3 Glacier
ルール2: 1日後、tag1付与のオブジェクトを失効
ルール3: 1日後、不完全なマルチパートアップロードを削除
-- 1. コマンド等のインストール
-- 1.1 aws cli version 2 インストール
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
-- 2. S3 バケットを作成する
aws s3 mb s3://bucket123
aws s3 ls
-- 3. テストファイルのアップロード
echo test01 > test01.txt
echo test02 > test02.txt
echo test03 > test03.txt
echo test04 > test04.txt
aws s3api put-object --bucket bucket123 --key 01/test01.txt --body test01.txt
aws s3api put-object --bucket bucket123 --key 01/test02.txt --body test02.txt
aws s3api put-object --bucket bucket123 --key 02/test03.txt --body test03.txt
aws s3api put-object --bucket bucket123 --key 02/test04.txt --body test04.txt
aws s3 ls s3://bucket123 --recursive
ストレージクラスの確認
aws s3api head-object --bucket bucket123 --key 01/test01.txt
aws s3api head-object --bucket bucket123 --key 01/test02.txt
aws s3api head-object --bucket bucket123 --key 02/test03.txt
aws s3api head-object --bucket bucket123 --key 02/test04.txt
→標準ストレージクラスの場合は何も表示されない
-- 4. タグの設定
aws s3api put-object-tagging \
--bucket bucket123 \
--key 02/test03.txt \
--tagging '{"TagSet": [{ "Key": "key1", "Value": "tag1" }]}'
aws s3api get-object-tagging \
--bucket bucket123 \
--key 02/test03.txt
{
"Rules": [
{
"ID": "rule01",
"Status": "Enabled",
"Filter": {
"Prefix": "01/"
},
"Transitions": [
{
"Days": 1,
"StorageClass": "GLACIER"
}
]
},
{
"ID": "rule02",
"Status": "Enabled",
"Filter": {
"Tag": {
"Key": "key1",
"Value": "tag1"
}
},
"Expiration": {
"Days": 1
}
},
{
"ID": "rule03",
"Status": "Enabled",
"Filter": {},
"AbortIncompleteMultipartUpload": {
"DaysAfterInitiation": 1
}
}
]
}
aws s3api put-bucket-lifecycle-configuration \
--bucket bucket123 \
--lifecycle-configuration file://a.json
aws s3api get-bucket-lifecycle-configuration \
--bucket bucket123
-- 6. 動作確認
1日待つ
aws s3 ls s3://bucket123 --recursive
ストレージクラスの確認
aws s3api head-object --bucket bucket123 --key 01/test01.txt
aws s3api head-object --bucket bucket123 --key 01/test02.txt
aws s3api head-object --bucket bucket123 --key 02/test03.txt
aws s3api head-object --bucket bucket123 --key 02/test04.txt
-- 7. クリーンアップ
-- バケットの削除
aws s3 ls
aws s3 rb s3://bucket123 --force