About ETags
An ETag (entity tag) is a content fingerprint that the server returns for every object. The response ofPutObject, HeadObject, and GetObject calls includes the ETag. When an object’s content changes, its ETag changes. Use HeadObject to retrieve the current ETag without downloading the object.
The server returns ETags as quoted hex strings, for example "1b2cf535f27731c97434645a985325". Include the quotes when you pass an ETag to a precondition header.
Conditional reads
Conditional reads let you skip aGetObject download when the object hasn’t changed since the last time you read it. Pass the ETag you last received as the If-None-Match value. If the object hasn’t changed, the server returns 304 Not Modified and transfers no data. If the object has changed, the download proceeds.
Use If-Match to download only when the object matches a specific ETag, or to detect that the object changed since you last read it.
- AWS CLI
- Boto3
Replace If the ETag matches (the object hasn’t changed), the command returns an error that wraps the
[BUCKET-NAME], [OBJECT-KEY], [LAST-KNOWN-ETAG], and [LOCAL-OUTPUT-PATH] with the appropriate values.304 Not Modified response.Conditional writes
Conditional writes prevent accidental overwrites by requiring that a precondition holds beforePutObject, CompleteMultipartUpload, CopyObject, or RenameObject modifies an object. If the precondition fails, the server returns 412 Precondition Failed and doesn’t modify the object. RenameObject supports a richer set of conditional headers, including separate source and destination preconditions and time-based conditions. See RenameObject conditionals for details.
Conditional writes support two patterns:
For full header constraints, see Conditional writes in the S3 API reference.
Safe create
UseIf-None-Match: * to write an object only if no object already exists at that key. This prevents overwriting existing data even when multiple writers race for the same key.
If two concurrent requests use If-None-Match: * for the same key, one succeeds and the other returns 409 ConditionalRequestConflict. Retry the request on 409.
- AWS CLI
- Boto3
Replace
[BUCKET-NAME], [OBJECT-KEY], and [LOCAL-FILE-PATH] with the appropriate values.Compare-and-swap
UseIf-Match with the current ETag to write an object only if it hasn’t changed since you last read it. This detects concurrent updates that occur between your read and write.
The workflow is:
- Call
HeadObjectto retrieve the current ETag. - Perform your local computation or modification.
- Call
PutObjectwithIf-Matchset to the ETag from step 1.
412 Precondition Failed. Retry from step 1 to read the updated object.
- AWS CLI
- Boto3
Replace
[BUCKET-NAME], [OBJECT-KEY], and [LOCAL-FILE-PATH] with the appropriate values.RenameObject conditionals
RenameObject supports a richer set of conditional headers, including separate preconditions for the source and destination objects, and time-based conditions. For the full list of supported headers and usage examples, see Preconditions and safeguards in the rename objects guide.