

# Use the Amazon SDK for Go v2 with Amazon services
<a name="use-services"></a>

 To make calls to an Amazon service, you must first construct a service client instance. A service client provides low-level access to every API action for that service. For example, you create an Amazon S3 service client to make calls to Amazon S3 APIs. 

 When you call service operations, you pass in input parameters as a struct. A successful call will result in an output struct containing the service API response. For example, after you successfully call an Amazon S3 create bucket action, the action returns an output struct with the bucket's location. 

 For the list of service clients, including their methods and parameters, see the [Amazon SDK for Go API Reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2). 

# Data integrity protection with checksums
<a name="s3-checksums"></a>

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](https://docs.amazonaws.cn/AmazonS3/latest/userguide/checking-object-integrity.html) including the [supported algorithms](https://docs.amazonaws.cn/AmazonS3/latest/userguide/checking-object-integrity.html#using-additional-checksums).

You have the flexibility to choose the algorithm that best fits your needs and let the SDK calculate the checksum. Alternatively, you can provide a pre-computed checksum value by using one of the supported algorithms. 

**Note**  
Beginning with version [v1.74.1 of the Amazon S3 module](https://github.com/aws/aws-sdk-go-v2/blob/v1.34.0/service/s3/CHANGELOG.md#v1741-2025-01-24), the SDK provides default integrity protections by automatically calculating a `CRC32` checksum for uploads. The SDK calculates this checksum if you don't provide a precalculated checksum value or if you don't specify an algorithm that the SDK should use to calculate a checksum.  
The SDK also provides global settings for data integrity protections that you can set externally, which you can read about in the [Amazon SDKs and Tools Reference Guide](https://docs.amazonaws.cn/sdkref/latest/guide/feature-dataintegrity.html).

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

## Upload an object
<a name="use-service-S3-checksum-upload"></a>

When you upload an object with the `putObject` method and provide a checksum algorithm, the SDK computes the checksum for the specified algorithm.

The following code snippet shows a request to upload an object with a `CRC32` checksum. When the SDK sends the request, it calculates the `CRC32` checksum and uploads the object. Amazon S3 validates the integrity of the content by calculating the checksum and comparing it to the checksum provided by the SDK. Amazon S3 then stores the checksum with the object.

```
out, err := s3Client.PutObject(context.Background(), &s3.PutObjectInput{
     Bucket:            aws.String("bucket"),
     Key:               aws.String("key"),
     ChecksumAlgorithm: types.ChecksumAlgorithmCrc32,
     Body:              strings.NewReader("Hello World"),
 })
```

If you don't provide a checksum algorithm with the request, the checksum behavior varies depending on the version of the SDK that you use as shown in the following table.

**Checksum behavior when no checksum algorithm is provided**


| Amazon S3 module version of Amazon SDK for Go | Checksum behavior | 
| --- | --- | 
| Earlier than v1.74.1 | The SDK doesn't automatically calculate a CRC-based checksum and provide it in the request. | 
| v1.74.1 or later |  The SDK uses the `CRC32` algorithm to calculate the checksum and provides it in the request. Amazon S3 validates the integrity of the transfer by computing its own `CRC32` checksum and compares it to the checksum provided by the SDK. If the checksums match, the checksum is saved with the object.   | 

### Use a pre-calculated checksum value
<a name="use-service-S3-checksum-upload-pre"></a>

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 SHA256 checksum.

```
out, err := s3Client.PutObject(context.Background(), &s3.PutObjectInput{
     Bucket:        aws.String("bucket"),
     Key:           aws.String("key"),
     ChecksumCRC32: aws.String("checksumvalue"),
     Body:          strings.NewReader("Hello World"),
 })
```

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

### Multipart uploads
<a name="use-service-S3-checksum-upload-multi"></a>

You can also use checksums with multipart uploads.

 The Amazon SDK for Go provides two options to use checksums with multipart uploads. The first option uses the transfer manager that specifies the `CRC32` algorithm for the upload.

```
s3Client := s3.NewFromConfig(cfg)
     transferManager := manager.NewUploader(s3Client)
     out, err := transferManager.Upload(context.Background(), &s3.PutObjectInput{
             Bucket:            aws.String("bucket"),
             Key:               aws.String("key"),
             Body:              large file to trigger multipart upload,
             ChecksumAlgorithm: types.ChecksumAlgorithmCrc32,
     })
```

If you don't provide a checksum algorithm when using the transfer manager for uploads, the SDK automatically calculates and checksum based on the `CRC32` algorithm. The SDK performs this calculation for all versions of the SDK.

The second option uses the [Amazon S3](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3) client to perform the multipart upload. If you specify a checksum with this approach, you must specify the algorithm to use on the initiation of the upload. You must also specify the algorithm for each part request and provide the checksum calculated for each part after it is uploaded.

```
s3Client := s3.NewFromConfig(cfg)
     createMultipartUploadOutput, err := s3Client.CreateMultipartUpload(context.Background(), &s3.CreateMultipartUploadInput{
             Bucket:            aws.String("bucket"),
             Key:               aws.String("key"),
             ChecksumAlgorithm: types.ChecksumAlgorithmCrc32,
          })
     if err != nil {
             log.Fatal("err create multipart upload ", err)
     }

     var partsBody []io.Reader // this is just an example parts content, you should load your target file in your code
     partNum := int32(1)
     var completedParts []types.CompletedPart
     for _, body := range partsBody {
         uploadPartOutput, err := s3Client.UploadPart(context.Background(), &s3.UploadPartInput{
             Bucket:            aws.String("bucket"),
             Key:               aws.String("key"),
             ChecksumAlgorithm: types.ChecksumAlgorithmCrc32,
             Body:              body,
             PartNumber:        aws.Int32(partNum),
             UploadId:          createMultipartUploadOutput.UploadId,
         })
         if err != nil {
             log.Fatal("err upload part ", err)
         }

         completedParts = append(completedParts, types.CompletedPart{
             PartNumber:    aws.Int32(partNum),
             ETag:          uploadPartOutput.ETag,
             ChecksumCRC32: uploadPartOutput.ChecksumCRC32,
         })
         partNum++
     }

     completeMultipartUploadOutput, err := s3Client.CompleteMultipartUpload(context.Background(), &s3.CompleteMultipartUploadInput{
         Bucket:   aws.String("bucket"),
         Key:      aws.String("key"),
         UploadId: createMultipartUploadOutput.UploadId,
         MultipartUpload: &types.CompletedMultipartUpload{
             Parts: completedParts,
         },
     })
     if err != nil {
         log.Fatal("err complete multipart upload ", err)
     }
```

## Download an object
<a name="use-service-S3-checksum-download"></a>

When you use the [GetObject](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#Client.GetObject) method to download an object, the SDK automatically validates the checksum when the `ChecksumMode` field of `GetObjectInput` is set to `types.ChecksumModeEnabled`. 

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

```
out, err := s3Client.GetObject(context.Background(), &s3.GetObjectInput{
     Bucket:       aws.String("bucket"),
     Key:          aws.String("key"),
     ChecksumMode: types.ChecksumModeEnabled,
 })
```

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