Covers key changes between OSS SDK for Go V1 and V2, including import paths, configuration, API operations, presigned URLs, resumable transfers, and encryption.
Minimum Go version
V2 requires Go 1.18 or later.
Import paths
V2 uses a new repository (alibabacloud-oss-go-sdk-v2) with code organized by functional module.
|
Module path |
Description |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss |
Core module for basic and advanced API operations. |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials |
Access credentials. |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/retry |
Retry policies. |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/signer |
Signatures. |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/transport |
HTTP clients. |
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/crypto |
Client-side encryption. |
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Configuration
-
V2 consolidates settings into config and provides With-prefixed helper functions for overrides.
-
V2 uses V4 signatures by default, so you must specify the region.
-
V2 derives the endpoint from the region. For public cloud access, no explicit endpoint is needed.
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Create a client
V2 replaces New with NewClient, which no longer accepts endpoint, AK, or SK as direct parameters.
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
API operations
V2 unifies each API into a single Client method named <OperationName>, with request type <OperationName>Request and response type <OperationName>Result. Each call requires a context.Context. Syntax:
func (c *Client) <OperationName>(ctx context.Context, request *<OperationName>Request, optFns ...func(*Options)) (*<OperationName>Result,, error)
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Presigned URLs
V2 renames the presigning method from SignURL to Presign and moves it to Client. Syntax:
func (c *Client) Presign(ctx context.Context, request any, optFns ...func(*PresignOptions)) (*PresignResult, error)
The request parameter type matches '<OperationName>Request' from the corresponding API.
The response includes the presigned URL, HTTP method, expiration time, and signed headers. Example:
type PresignResult struct {
Method string
URL string
Expiration time.Time
SignedHeaders map[string]string
}
The following examples generate a presigned download URL in V1 and V2:
Example of OSS SDK for Go V1
Example of OSS SDK for Go V2
Resumable transfers
V2 replaces the V1 resumable methods (Bucket.UploadFile, Bucket.DownloadFile, Bucket.CopyFile) with transfer managers: Uploader, Downloader, and Copier.
The following table maps V1 methods to V2 equivalents.
|
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 |
When an object exceeds the size threshold, V2 automatically uses multipart upload, download, or copy.
Client-side encryption
V2 introduces EncryptionClient for client-side encryption, following the same naming conventions and calling patterns as Client. V2 provides examples only for RSA-based, self-managed CMKs.
A KMS-based example is available at sample/crypto/kms.go.
The following examples upload an object with RSA-based CMK encryption in V1 and V2:
V1
V2
Configure retry policies
V2 retries HTTP requests by default. Remove any custom retry logic from your V1 code to avoid excessive retries.