ãAWSãCLIãããã¼ã¸ã§ãã³ã°ãããS3ãã±ãããåé¤ããæ¹æ³
S3ãã±ããã¯ç©ºã«ãã¦ããã§ãªãã¨åé¤ã§ãã¾ãããããã¯ãã¼ã¸ã§ãã³ã°ããããã±ããã§ãåæ§ã§ãã
åé¡ã¯ããã¼ã¸ã§ãã³ã°ããããªãã¸ã§ã¯ããåé¤ãã¦ããå®ã¯æ¶ãã¦ããªããã¨ã§ããDeleteMarkerã追å ãããã ãã§ããªãã¸ã§ã¯ããå®å ¨ã«æ¶ããããã§ã¯ããã¾ãããå®å ¨ã«åé¤ããããã«ã¯ãDeleteMarkerã¨ãã¼ã¸ã§ã³ãåé¤ããå¿ è¦ãããã¾ãã
ããã¼ã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§ããã°åé¤ããã®ã¯é£ããããã¾ãããããããAWS CLIããå°ã é¢åãªã®ã§æé ãã¾ã¨ãã¾ããã
[1] ãªãã¸ã§ã¯ãã®åé¤
[1-1] ãã±ããã¨ãã¡ã¤ã«ã®ä½æ
試ãã«ãã¼ã¸ã§ãã³ã°ããããã±ãããä½æããtest.txtãã¢ãããã¼ããã¾ãã
$ aws s3 ls s3://{ãã±ããå} 2021-04-17 13:19:12 4 test.txt
ãã¼ã¸ã§ã³ã確èªãã¾ãã
$ aws s3api list-object-versions --bucket {ãã±ããå} { "Versions": [ { "LastModified": "2021-04-17T13:19:12.000Z", "VersionId": "Goie_timWFWXJTwHv9swGAyjbIrDI6cQ", "ETag": "\"033bd94b1168d7e4f0d644c3c95e35bf\"", "StorageClass": "STANDARD", "Key": "test.txt", "Owner": { "DisplayName": "{ææè ã®è¡¨ç¤ºå}", "ID": "{ææè ã®ID}" }, "IsLatest": true, "Size": 4 } ] }
[1-2] ãªãã¸ã§ã¯ãã®åé¤
aws s3 rm
ã§ãªãã¸ã§ã¯ããåé¤ãã¾ãã
$ aws s3 rm s3://{ãã±ããå}/ --recursive delete: s3://{ãã±ããå}/test.txt
aws s3 ls
ã§ç¢ºèªããã¨åé¤ãããããã«è¦ãã¾ãã
$ aws s3 ls s3://{ãã±ããå}
ãããããã±ãããåé¤ãããã¨ããã¨ã¨ã©ã¼ãçºçãã¾ãã
$ aws s3 rb s3://{ãã±ããå} --force remove_bucket failed: s3://{ãã±ããå} An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
[1-3] ãã¼ã¸ã§ã³ã®ç¢ºèª
ãã¼ã¸ã§ã³ã確èªããã¨ãå®å ¨ã«ã¯åé¤ããã¦ããªããã¨ãåããã¾ããDeleteMarkersã追å ããã¦ãã¾ãã
$ aws s3api list-object-versions --bucket {ãã±ããå} { "DeleteMarkers": [ { "Owner": { "DisplayName": "{ææè ã®è¡¨ç¤ºå}", "ID": "{ææè ã®ID}" }, "IsLatest": true, "VersionId": "mfYZIOM57MlB_8BkvG7YcNuKWJz0geHS", "Key": "test.txt", "LastModified": "2021-04-17T13:35:10.000Z" } ], "Versions": [ { "LastModified": "2021-04-17T13:19:12.000Z", "VersionId": "Goie_timWFWXJTwHv9swGAyjbIrDI6cQ", "ETag": "\"033bd94b1168d7e4f0d644c3c95e35bf\"", "StorageClass": "STANDARD", "Key": "test.txt", "Owner": { "DisplayName": "{ææè ã®è¡¨ç¤ºå}", "ID": "{ææè ã®ID}" }, "IsLatest": false, "Size": 4 } ] }
"DeleteMarkers", "Versions"ãåé¤ããå¿ è¦ãããã¾ãã
[2] å®å ¨ãªåé¤
以ä¸ã®ã·ã§ã«ã¹ã¯ãªãããä½æã»å®è¡ãã¦åé¤ãã¾ããã³ãã³ãã¯ä¸è¨è¨äºãåèã«ããã¦ããã ãã¾ããã
バージョニングが有効なS3バケットをAWS CLIで空にする手順(オブジェクト1000個以下) - のぴぴのメモ
#!/bin/bash BUCKET_NAME={ãã±ããå} # ãã±ããå ã®å ¨ãªãã¸ã§ã¯ãåé¤ aws s3 rm s3://${BUCKET_NAME}/ --recursive # DeleteMarkersã®åé¤ aws s3api list-object-versions --bucket ${BUCKET_NAME} \ | jq -r -c '.["DeleteMarkers"][] | [.Key,.VersionId]' \ | while read line do key=`echo $line | jq -r .[0]` versionid=`echo $line | jq -r .[1]` aws s3api delete-object --bucket ${BUCKET_NAME} \ --key ${key} --version-id ${versionid} done # Versionsã®åé¤ aws s3api list-object-versions --bucket ${BUCKET_NAME} \ | jq -r -c '.["Versions"][] | [.Key,.VersionId]' \ | while read line do key=`echo $line | jq -r .[0]` versionid=`echo $line | jq -r .[1]` aws s3api delete-object --bucket ${BUCKET_NAME} \ --key ${key} --version-id ${versionid} done
å®è¡ããã¨ã以ä¸ã®ãããªçµæãè¿ã£ã¦ãã¾ãã
$ sh delete_s3_object.sh { "VersionId": "mfYZIOM57MlB_8BkvG7YcNuKWJz0geHS", "DeleteMarker": true } { "VersionId": "Goie_timWFWXJTwHv9swGAyjbIrDI6cQ" }
ãã¼ã¸ã§ã³ãå度確èªããã¨ãä»åº¦ã¯åé¤ããã¦ãã¾ãã
$ aws s3api list-object-versions --bucket {ãã±ããå}
ãã±ããåé¤ãå®è¡ããã¨ä»åº¦ã¯åé¤ã«æåãã¾ãã
$ aws s3 rb s3://{ãã±ããå} --force remove_bucket: {ãã±ããå}
åºå ¸
- ã¢ã¤ãã£ããã¯Clker-Free-Vector-Imagesã«ããPixabayããã®ç»å