This topic describes how to upgrade Object Storage Service (OSS) SDK for Go from V1 to V2.
Earliest version for Go
OSS SDK for Go V2 requires that the version of Go must be 1.18 or later.
Specify import paths
OSS SDK for Go V2 uses a new code repository, alibabacloud-oss-go-sdk-v2. The code structure is modified and organized by functional module. The following table describes the paths and descriptions of the functional modules.
Module path | Description |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss | The core of OSS SDK for Go, which is used to call basic and advance API operations. |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials | The access credentials. |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/retry | The retry policies. |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/signer | The signatures. |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/transport | The HTTP clients. |
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/crypto | The client-side encryption configurations. |
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Configure parameters
OSS SDK for Go V2 simplifies configurations and imports the configurations to config. OSS SDK for Go V2 provides auxiliary functions prefixed with With to facilitate the programmatic overwriting of the default configurations.
OSS SDK for Go V2 uses V4 signatures by default. In this case, you must specify the region.
OSS SDK for Go V2 allows you to create an endpoint based on the region information. If you access resources in the public cloud, you do not need to create an endpoint.
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Create a client
In OSS SDK for Go V2, the client creation function is changed from New to NewClient. In addition, the client creation function no longer supports the endpoint, ak, and sk parameters.
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Call API operations
Basic API operations are merged into a single operation method in the <OperationName> format, the request parameters of an operation are merged into <OperationName>Request, and the response parameters of an operation are merged into <OperationName>Result. The operation methods are imported to Client, and context.Context needs to be specified at the same time. Syntax:
func (c *Client) <OperationName>(ctx context.Context, request *<OperationName>Request, optFns ...func(*Options)) (*<OperationName>Result,, error)
For more information, see Basic API operations.
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Generate a pre-signed URL
In OSS SDK for Go V2, the name of the operation used to generate a pre-signed URL is changed from SignURL to Pressign, and the operation is imported to Client. Syntax:
func (c *Client) Presign(ctx context.Context, request any, optFns ...func(*PresignOptions)) (*PresignResult, error)
The type of request parameters is the same as '<OperationName>Request' in the API operation.
The response contains a pre-signed URL, the HTTP method, the expiration time of the URL, and the signed request headers. Example:
type PresignResult struct {
Method string
URL string
Expiration time.Time
SignedHeaders map[string]string
}
For more information, see Pre-signed URL.
The following sample code provides an example on how to migrate an object from OSS SDK for Go V1 to OSS SDK for Go V2 by generating a pre-signed URL that is used to download the object:
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Call resumable transfer operations
OSS SDK for Go V2 uses data transmission managers, including Uploader, Downloader, and Copier, to manage the upload, download, and copying of objects respectively. The original resumable transfer operations (Bucket.UploadFile, Bucket.DownloadFile, and Bucket.CopyFile) are deleted.
The following table describes the resumable transfer operations in OSS SDK for Go V1 and OSS SDK for Go V2.
Scenario | v2 | v1 |
Upload an object | Uploader.UploadFile | Bucket.UploadFile |
Upload a stream (io.Reader) | Uploader.UploadFrom | Not supported |
Download an object to a local computer | Downloader.DownloadFile | Bucket.DownloadFile |
Copy an object | Copier.Copy | Bucket.CopyFile |
Changes to default values
Scenario | v2 | v1 |
Object upload-part size | 6 MiB | Configure part size by specifying parameters |
Object upload-default value for concurrency | 3 | 1 |
Object upload-size threshold | The size of the part | None |
Object upload-record upload progress in the checkpoint file | Supported | Supported |
Object download-part size | 6 MiB | Configure part size by specifying parameters |
Object download-default value for concurrency | 3 | 1 |
Object download-size threshold | The size of the part | None |
Object download-record download progress in the checkpoint file | Supported | Supported |
Object copy-part size | 64 MiB | Bucket.UploadFile |
Object copy-default value for concurrency | 3 | 1 |
Object copy-size threshold | 200 MiB | None |
Object copy-record copy progress in the checkpoint file | Not supported | Supported |
The object upload-size threshold, object download-size threshold, or object copy-size threshold parameters listed above specify that when the object size is greater than the value of the parameters, multipart upload, multipart download, or multipart copy, respectively, is performed.
For more information about how to use data transmission managers, see Transfer managers.
Perform client-side encryption
OSS SDK for Go V2 uses EncryptionClient to provide client encryption. OSS SDK for Go V2 also simplifies the API operations used to perform client-side encryption and adopts the same operation naming rules and calling methods as Client. In addition, OSS SDK for Go V2 only provides reference on how to perform client-side encryption by using an RSA-based, self-managed CMK.
For more information about how to perform client-side encryption by using Key Management Service (KMS), visit sample/crypto/kms.go.
For more information about client-side encryption, see Client-side encryption.
The following sample code provides examples on how to use an RSA-based CMK to perform client-side encryption when you upload an object by using OSS SDK for Go V1 and OSS SDK for Go V2:
V1
V2
Configure retry policies
By default, OSS SDK for Go V2 supports retries for HTTP requests. When you upgrade OSS SDK for Go V1 to OSS SDK for Go V2, you must remove the original retry code to prevent an increase in the number of retries.
Reference
For more information, see Developer Guide.