Understanding ETag Formats in Amazon S3

Aparna VikramanAparna Vikraman
4 min read

Introduction

Amazon S3 (Simple Storage Service) is a popular cloud storage service offered by Amazon Web Services (AWS). It provides a scalable, reliable, and secure infrastructure for storing and retrieving data. In this blog post, we'll explore the concept of E-tag in S3.

What is an ETag in Amazon S3?

An ETag is a unique identifier assigned to an object when it is uploaded to Amazon S3. It represents the object's content and is generated automatically by S3. The ETag is considered immutable and cannot be directly updated or modified. The ETag can have different formats depending on how the object was uploaded.

ETag-1: Meaning and significance as an MD5 hash.

ETag-1 format represents the MD5 hash of the object's content. It is typically generated for objects uploaded using a single PUT operation. It can be used to verify the integrity of the object by comparing it with the MD5 hash of the downloaded file.

ETag-2: Usage for multipart uploads.

ETag-2 format represents a multipart ETag. It is generated when an object is uploaded using a multipart upload process. It consists of the concatenation of individual ETags of each uploaded part, separated by a dash ("-"). This format helps identify the parts that make up the complete object.

Does the ETag format affect object retrieval?

The ETag format does not directly affect the retrieval of an object from Amazon S3. Whether it is ETag-1 or ETag-2, it does not impact the ability to retrieve the object using the appropriate methods.

How to upload an object of ETag-1 format?

To upload an object with an ETag format of ETag-1, you can use the aws s3api put-object command with the --content-md5 option. The --content-md5 option allows you to provide the MD5 hash of the object's content, which is used to calculate the ETag.

Here's an example command to upload an object with an ETag format of ETag-1:

aws s3api put-object --bucket mybucket --key myobject --body file.txt --content-md5 $(openssl dgst -md5 -binary file.txt | openssl enc -base64)

In the above command:

  • mybucket is the name of the S3 bucket.

  • myobject is the desired key or path for the object within the bucket.

  • file.txt is the path to the file you want to upload.

  • $(openssl dgst -md5 -binary file.txt | openssl enc -base64) calculates the MD5 hash of the file's content and encodes it in base64. This value is used as the --content-md5 parameter.

When you upload the object using this command, the ETag value of the uploaded object will be the MD5 hash of the object's content, resulting in an ETag format of ETag-1.

Please note that generating the MD5 hash and providing it as the --content-md5 option ensures that the ETag matches the content of the file. It's important to calculate the MD5 hash correctly to ensure data integrity.

How to upload an object of ETag-2 format?

To upload an object with an ETag-2 format, you need to perform a multipart upload in Amazon S3. A multipart upload allows you to upload large objects in parts, which can improve performance and reliability. Each part is assigned an individual ETag, and the ETag of the completed multipart upload is formed by concatenating the individual ETags.

Here's an example of how to perform a multipart upload using the AWS CLI:

  1. Initiate the multipart upload:
aws s3api create-multipart-upload --bucket mybucket --key myobject --content-type "application/octet-stream"

This command initiates a multipart upload for the specified bucket and key. It returns an UploadId that you will need for subsequent steps.

  1. Upload the parts
aws s3api upload-part --bucket mybucket --key myobject --part-number 1 --body part1.txt --upload-id <UploadId>
aws s3api upload-part --bucket mybucket --key myobject --part-number 2 --body part2.txt --upload-id <UploadId>

Upload each part of the object using the upload-part command. Specify the part number, the path to the file for each part, and the UploadId obtained from the previous step.

  1. Complete the multipart upload:
aws s3api complete-multipart-upload --bucket mybucket --key myobject --upload-id <UploadId> --multipart-upload '{"Parts": [{"ETag": "<ETag1>", "PartNumber": 1}, {"ETag": "<ETag2>", "PartNumber": 2}]}'

Complete the multipart upload using the complete-multipart-upload command. Provide the Upload Id and specify the ETags of each uploaded part along with their corresponding part numbers. Upon successful completion of the multipart upload, the ETag of the uploaded object will be in the ETag-2 format, which represents the concatenated ETags of the individual parts.

Note: Multipart uploads are typically used for objects larger than 5 MB in size. For smaller objects, a single PUT operation will result in an ETag-1 format.

Conclusion

In this blog post, we explored the concept of ETag (Entity Tag) formats in Amazon S3 and gained a better understanding of their significance. ETags play a crucial role in identifying and verifying the integrity of objects stored in S3.

0
Subscribe to my newsletter

Read articles from Aparna Vikraman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aparna Vikraman
Aparna Vikraman