Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PutObject If-Match PreconditionFailed not functional #8653

Open
zeenewton opened this issue Mar 5, 2025 · 0 comments
Open

PutObject If-Match PreconditionFailed not functional #8653

zeenewton opened this issue Mar 5, 2025 · 0 comments
Labels

Comments

@zeenewton
Copy link

Hello 👋
Just noticed that I'm not getting an exception when attempting to put some s3 behavior under test.

Previously, I think PutObject only support If-None-Match conditional header to prevent overwrites, but I noticed they added If-Match support which functions similarly to GetObject/HeadObject (raising if etag doesn't match header).

I'm able to workaround for now by adding a HeadObject call before my put, but not ideal to add calls and change code to support behavior.

Problem and recreation steps:

import boto3
import moto
import pytest
from botocore.exceptions import ClientError

def test_put_if_match():
    """fails"""
    with pytest.raises(ClientError) as exc:
        with moto.mock_aws():
            s3 = boto3.client("s3", region_name="us-east-1")
            s3.create_bucket(Bucket="test_bucket")
            s3.put_object(Bucket="test_bucket", Key="test_key", Body=b"test")
            s3.put_object(Bucket="test_bucket", Key="test_key", Body=b"test2", IfMatch="test")
    err = exc.value.response["Error"]
    assert err["Code"] == "PreconditionFailed"

def test_wo_moto():
    """passes with real bucket/client"""
    with pytest.raises(ClientError) as exc:
        s3 = boto3.client("s3", region_name="us-east-1")
        s3.create_bucket(Bucket="my-redacted-bucket")
        s3.put_object(Bucket="my-redacted-bucket", Key="test_key", Body=b"test")
        s3.put_object(Bucket="my-redacted-bucket", Key="test_key", Body=b"test2", IfMatch="test")
    err = exc.value.response["Error"]
    assert err["Code"] == "PreconditionFailed"
@bblommers bblommers added the bug label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants