あめがえるのITブログ

頑張りすぎない。ほどほどに頑張るブログ。

【AWS】 11/25(月) Amazon S3 が条件付き書き込みの新機能追加


AWS】 2024/11/25(月) Amazon S3 が条件付き書き込みの新機能追加
aws.amazon.com

サポートされるAPI

・GetObject、HeadObject、PutObject、DeleteObjectなど
 ・If-Match:ETagが指定されたものと一致する場合にのみオブジェクトを返す
 ・If-Modified-Since:指定された時刻以降に変更された場合にのみオブジェクトを返す
 ・If-None-Match:ETagが指定されたものと一致しない場合にのみオブジェクトを返す
 ・If-Unmodified-Since:指定された時刻以降に変更されていない場合にのみオブジェクトを返す
・CopyObject
 ・x-amz-copy-source-if-match:ETagが指定されたものと一致する場合にのみソースオブジェクトを返す
 ・x-amz-copy-source-if-modified-since:指定された時刻以降に変更された場合にのみソースオブジェクトを返す
 ・x-amz-copy-source-if-none-match:ETagが指定されたものと一致しない場合にのみソースオブジェクトを返す
 ・x-amz-copy-source-ifunmodified-since:指定された時刻以降に変更されていない場合にのみソースオブジェクトを返す

実践!

1.PUTを実行しオブジェクトが存在している場合としていない場合で挙動を確認する
1-1.CloudShellを起動し下記を実行

touch testfile.txt
aws s3api put-object --bucket <your-bucket-name> --key testfile.txt --body testfile.txt --if-none-match "*"
aws s3api put-object --bucket <your-bucket-name> --key testfile.txt --body testfile.txt --if-none-match "*"
※同じコマンドを2回実行する。1回目はオブジェクトが存在しないので作成できるが、2回目は作成済みのためエラーになる。
[cloudshell-user@ip-10-130-45-9 ~]$ touch testfile.txt
[cloudshell-user@ip-10-130-45-9 ~]$ aws s3api put-object --bucket test123456789999bbb --key testfile.txt --body testfile.txt --if-none-match "*"
{
    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
    "ServerSideEncryption": "AES256"
}
[cloudshell-user@ip-10-130-45-9 ~]$ aws s3api put-object --bucket test123456789999bbb --key testfile.txt --body testfile.txt --if-none-match "*"

An error occurred (PreconditionFailed) when calling the PutObject operation: At least one of the pre-conditions you specified did not hold