kakakakakku blog

Weekly Tech Blog: Keep Learning!

LocalStack for AWS 2026.06.0 の新機能!Amazon S3 の conditional deletes(条件付き削除)を試す

はじめに

LocalStack for AWS 2026.06.0 のアップデートを見ていたら Amazon S3 の conditional deletes(条件付き削除)がサポートされていた💡DeleteObject と DeleteObjects で If-Match ヘッダーを使ってオブジェクトを削除できる.

Conditional deletes are supported for DeleteObject and DeleteObjects via the IfMatch precondition, matching the S3 conditional deletes feature.

blog.localstack.cloud

ちなみに LocalStack は Amazon S3 の conditional writes(条件付き書き込み)もサポートしていて,それは2024年に試した.

kakakakakku.hatenablog.com

さっそく試していく \( 'ω')/

環境

  • LocalStack Ultimate プラン
  • LocalStack 2026.06.0

1. AWS で試す🌍️

まずは AWS で試す.

Amazon S3 バケットを作る

$ BUCKET=kakakakakku-sandbox-conditional-deletes
$ aws s3api create-bucket --bucket ${BUCKET} --create-bucket-configuration LocationConstraint=ap-northeast-1
{
    "Location": "http://kakakakakku-sandbox-conditional-deletes.s3.amazonaws.com/",
    "BucketArn": "arn:aws:s3:::kakakakakku-sandbox-conditional-deletes"
}

サンプルファイルをアップロードする

$ aws s3api put-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --body Arch_Amazon-Simple-Storage-Service_64.png
{
    "ETag": "\"9f1920d5f786cc12d9d3f98a56dea5cc\"",
    "ChecksumCRC64NVME": "i/TlEzuFR8I=",
    "ChecksumType": "FULL_OBJECT",
    "ServerSideEncryption": "AES256"
}

誤った ETag で削除する

取得した ETag の1文字目を変えて --if-match オプションに指定する.すると条件付き削除でエラーになった.

$ aws s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '"0f1920d5f786cc12d9d3f98a56dea5cc"'

aws: [ERROR]: An error occurred (PreconditionFailed) when calling the DeleteObject operation: At least one of the pre-conditions you specified did not hold

Additional error details:
Condition: If-Match

ちなみに API から直接 XML 形式のレスポンスを取得すると,HTTP ステータスコードは 412・エラーコードは PreconditionFailed になっていた👌

$ curl -s -w "HTTP_STATUS: %{http_code}\n" -X DELETE \
    --aws-sigv4 "aws:amz:ap-northeast-1:s3" \
    --user "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" \
    -H 'If-Match: "0f1920d5f786cc12d9d3f98a56dea5cc"' \
    "https://kakakakakku-sandbox-conditional-deletes.s3.ap-northeast-1.amazonaws.com/Arch_Amazon-Simple-Storage-Service_64.png"
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>PreconditionFailed</Code><Message>At least one of the pre-conditions you specified did not hold</Message><Condition>If-Match</Condition><RequestId>GS4FVM22PBKS535V</RequestId><HostId>7qpm4Gr7QfMpTvdyo0JPI36zZKdqYGs5KJW6sjRl9wH+a6avGOGVKGoOlV0HhLngLaaBlT394OE=</HostId></Error>HTTP_STATUS: 412

正しい ETag で削除する

今度は取得した正しい ETag を --if-match オプションに指定する.すると削除できた👌

$ aws s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '"9f1920d5f786cc12d9d3f98a56dea5cc"'

$ aws s3api head-object --bucket ${BUCKET} --key Arch_Amazon-Simple-Storage-Service_64.png

aws: [ERROR]: An error occurred (404) when calling the HeadObject operation: Not Found

If-Match に * を指定して存在するときに削除をする

--if-match '*' を指定するとオブジェクトが存在するかどうかを確認して,存在すれば削除するような挙動になる.既にオブジェクトは消してあるため NoSuchKey エラーになった.通常の DeleteObject だと存在しないキーでもレスポンスコード 204 で成功するため,ちゃんと conditional deletes(条件付き削除)の挙動になっていた.

$ aws s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '*'

aws: [ERROR]: An error occurred (NoSuchKey) when calling the DeleteObject operation: The specified key does not exist.

Additional error details:
Key: Arch_Amazon-Simple-Storage-Service_64.png

2. LocalStack で試す🌍️

今度は LocalStack で試す.aws コマンドを awslocal コマンドに置き換えれば OK👌

Amazon S3 バケットを作る

$ BUCKET=kakakakakku-sandbox-conditional-deletes

$ awslocal s3api create-bucket --bucket ${BUCKET} --create-bucket-configuration LocationConstraint=ap-northeast-1
{
    "Location": "http://kakakakakku-sandbox-conditional-deletes.s3.localhost.localstack.cloud:4566/",
    "BucketArn": "arn:aws:s3:::kakakakakku-sandbox-conditional-deletes"
}

サンプルファイルをアップロードする

$ awslocal s3api put-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --body Arch_Amazon-Simple-Storage-Service_64.png
{
    "ETag": "\"9f1920d5f786cc12d9d3f98a56dea5cc\"",
    "ChecksumCRC64NVME": "i/TlEzuFR8I=",
    "ChecksumType": "FULL_OBJECT",
    "ServerSideEncryption": "AES256"
}

誤った ETag で削除する

取得した ETag の1文字目を変えて --if-match オプションに指定する.すると条件付き削除でエラーになった.

$ awslocal s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '"0f1920d5f786cc12d9d3f98a56dea5cc"'

aws: [ERROR]: An error occurred (PreconditionFailed) when calling the DeleteObject operation: At least one of the pre-conditions you specified did not hold

Additional error details:
Condition: If-Match

ちなみに API から直接 XML 形式のレスポンスを取得すると,HTTP ステータスコードは 412・エラーコードは PreconditionFailed になっていた👌

$ curl -s -w "HTTP_STATUS: %{http_code}\n" -X DELETE \
    -H 'If-Match: "0f1920d5f786cc12d9d3f98a56dea5cc"' \
    "http://localhost:4566/${BUCKET}/Arch_Amazon-Simple-Storage-Service_64.png"
<?xml version='1.0' encoding='utf-8'?>
<Error><Code>PreconditionFailed</Code><Message>At least one of the pre-conditions you specified did not hold</Message><RequestId>ca83bbcc-a246-4932-92c7-75573c6cc189</RequestId><Condition>If-Match</Condition></Error>HTTP_STATUS: 412

正しい ETag で削除する

今度は取得した正しい ETag を --if-match オプションに指定する.すると削除できた👌

$ awslocal s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '"9f1920d5f786cc12d9d3f98a56dea5cc"'

$ awslocal s3api head-object --bucket ${BUCKET} --key Arch_Amazon-Simple-Storage-Service_64.png

aws: [ERROR]: An error occurred (404) when calling the HeadObject operation: Not Found

If-Match に * を指定して存在するときに削除をする

--if-match '*' を指定するとオブジェクトが存在するかどうかを確認して,存在すれば削除するような挙動になる.既にオブジェクトは消してあるため NoSuchKey エラーになった.通常の DeleteObject だと存在しないキーでもレスポンスコード 204 で成功するため,ちゃんと conditional deletes(条件付き削除)の挙動になっていた.

$ awslocal s3api delete-object --bucket ${BUCKET} \
    --key Arch_Amazon-Simple-Storage-Service_64.png \
    --if-match '*'

aws: [ERROR]: An error occurred (NoSuchKey) when calling the DeleteObject operation: The specified key does not exist.

Additional error details:
Key: Arch_Amazon-Simple-Storage-Service_64.png

まとめ

LocalStack for AWS 2026.06.0 でサポートされた Amazon S3 の conditional deletes(条件付き削除)を試してみた❗️

AWS と LocalStack どちらも試してみて同じ挙動になっていた.Amazon S3 の conditional deletes(条件付き削除)を使っているアプリケーションコードをローカル環境で確認するような用途で使えそう.

関連ポスト