Amazon S3 checksums with AWS SDK for PHP Version 3 - AWS SDK for PHP

Amazon S3 checksums with AWS SDK for PHP Version 3

Amazon Simple Storage Service (Amazon S3) provides the ability to specify a checksum when you upload an object. When you specify a checksum, it is stored with the object and can be validated when the object is downloaded.

Checksums provide an additional layer of data integrity when you transfer files. With checksums, you can verify data consistency by confirming that the received file matches the original file. For more information about checksums with Amazon S3, see the Amazon Simple Storage Service User Guide.

Amazon S3 currently supports four checksum algorithms: SHA-1, SHA-256, CRC-32, and CRC-32C. You have the flexibility to choose the algorithm that best fits your needs and let the SDK calculate the checksum. Alternatively, you can specify their own pre-computed checksum value by using one of the four supported algorithms.

Important

To work with the CRC-32C algorithm, your PHP environment requires the installation of the AWS Common Runtime (AWS CRT) extension.

We discuss checksums in two request phases: uploading an object and downloading an object.

Upload an object

You upload objects to Amazon S3 by using the putObject method of the S3Client. Use the ChecksumAlgorithm pair in the parameter array to enable checksum computation and specify the algorithm. Valid values for the algorithm are CRC32, CRC32C, SHA1, and SHA256.

The following code snippet shows a request to upload an object with a CRC-32 checksum. When the SDK sends the request, it calculates the CRC-32 checksum and uploads the object. Amazon S3 stores the checksum with the object.

$client = new \Aws\S3\S3Client(['region' => 'us-east-2']); // See the note below. $result = $client->putObject([ 'Bucket' => 'bucketname', 'Key' => 'key', 'ChecksumAlgorithm' => 'CRC32', 'Body' => 'Object contents to test the checksum.' ]);

If the checksum that the SDK calculates doesn't match the checksum that Amazon S3 calculates when it receives the request, an error is returned.

Note

Since version 3.277.10 of the SDK for PHP, 'version' is no longer a required parameter in a service client constructor. If 'version' is not provided, the value defaults to 'latest'.

Use a pre-calculated checksum value

A pre-calculated checksum value provided with the request disables automatic computation by the SDK and uses the provided value instead.

The following example shows a request with a pre-calculated SHA-256 checksum.

use Aws\S3\S3Client; use GuzzleHttp\Psr7; $client = new S3Client([ 'region' => 'us-east-1', ]); // Calculate the SHA-256 checksum of the contents to be uploaded. $contents = 'Object contents to test the checksum.'; $body = Psr7\Utils::streamFor($contents); $sha256 = base64_encode(Psr7\Utils::hash($body, 'sha256', true)); $result = $client->putObject([ 'Bucket' => 'bucketname', 'Key' => 'key', 'Body' => $body, 'ChecksumSHA256' => $sha256 ]);

If Amazon S3 determines the checksum value is incorrect for the specified algorithm, the service returns an error response.

Multipart uploads

You can also use checksums with multipart uploads. As shown in the following example, specify the checksum algorithm as a key-value pair in the params array of the MultipartUploader constructor.

$s3Client = new S3Client([ 'region' => 'us-east-1' ]); $stream = fopen("/path/to/large/file", "r"); $mpUploader = new MultipartUploader($s3Client, $stream, [ 'bucket' => 'bucketname', 'key' => 'key', 'params' => ['ChecksumAlgorithm' => 'CRC32'] ]);

Download an object

When you use the getObject method to download an object, the SDK automatically validates the checksum when the ChecksumMode key's value is enabled.

The request in the following snippet directs the SDK to validate the checksum in the response by calculating the checksum and comparing the values.

$result = $client->getObject([ 'Bucket' => 'remiss-us-east-2', 'Key' => 'test-checksum', 'ChecksumMode' => 'enabled', ]);

If the object wasn't uploaded with a checksum, no validation takes place.

An object in Amazon S3 can have multiple checksums, but only one checksum is validated on download. The following precedence— based on the efficiency of the checksum algorithm—determines which checksum the SDK validates:

  1. CRC-32C

  2. CRC-32

  3. SHA-1

  4. SHA-256

For example, if a response contains both CRC-32 and SHA-256 checksums, only the CRC-32 checksum is validated.