{GCP Cloud Storage}オブジェクトのライフサイクル管理

 

https://cloud.google.com/storage/docs/lifecycle?hl=ja

https://cloud.google.com/storage/docs/managing-lifecycles?hl=ja

https://dev.classmethod.jp/articles/gcs_lifecycle/


ライフサイクル ルールには、次のアクションのいずれか 1 つのみを指定します。
Delete
SetStorageClass
AbortIncompleteMultipartUpload


ライフサイクル ルールでは、次の条件がサポートされています。
age
createdBefore
customTimeBefore
daysSinceCustomTime
daysSinceNoncurrentTime
isLive
matchesStorageClass
matchesPrefix と matchesSuffix
noncurrentTimeBefore
numNewerVersions

ライフサイクルの構成を変更した場合、変更後 24 時間が経過するまで、
オブジェクトのライフサイクル管理が古い構成を使用して操作を実行する可能性があります。


Age is measured from the resource's creation time.
For objects, the creation time is the time when the object is successfully written to the bucket, such as when an upload completes.


age条件で1日経過後に削除するポリシーを作成

 

-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update

-- 2. バケットの作成

gcloud storage buckets create gs://bucket123 \
--default-storage-class=Standard \
--no-enable-autoclass \
--location=asia-northeast1 \
--public-access-prevention \
--uniform-bucket-level-access

gcloud storage ls


-- 3. ファイルのアップロード


echo test > test.txt
gcloud storage cp test.txt gs://bucket123

gcloud storage ls --long gs://bucket123/*

 

-- 4. バケットのライフサイクル構成を設定する


vim policy.json

{
  "lifecycle": {
    "rule": [
      {
        "action": {"type": "Delete"},
        "condition": {
          "age": 1
        }
      }
    ]
  }
}

cat policy.json

gcloud storage buckets update gs://bucket123 --lifecycle-file=policy.json

gcloud storage buckets describe gs://bucket123
gcloud storage buckets describe gs://bucket123 --format="default(lifecycle)"

 

-- 5. 動作確認


gcloud storage ls --long gs://bucket123/*

1-2日待つ。BLOBが削除されていることを確認


-- 6. クリーンアップ

gcloud storage rm gs://bucket123 --recursive
gcloud storage ls

 

gcloud projects list

gcloud projects delete project01-9999999 \
--quiet