All Products
Search
Document Center

Object Storage Service:Transfer acceleration (Go SDK V2)

Last Updated:Mar 20, 2026

Transfer acceleration reduces latency for users accessing Object Storage Service (OSS) buckets across long geographical distances. Use it when your clients are distributed worldwide or when uploading and downloading objects that are gigabytes or terabytes in size.

Prerequisites

Before you begin, make sure you have:

Note: The sample code uses the China (Hangzhou) region (cn-hangzhou) and the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For endpoint details, see OSS regions and endpoints.

Enable transfer acceleration

The following example creates an OSS client, builds a PutBucketTransferAccelerationRequest with Enabled set to true, and calls PutBucketTransferAcceleration to enable transfer acceleration for the specified bucket.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to enable transfer acceleration for the bucket.
	request := &oss.PutBucketTransferAccelerationRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		TransferAccelerationConfiguration: &oss.TransferAccelerationConfiguration{
			Enabled: oss.Ptr(true), // Enable transfer acceleration.
		},
	}

	// Execute the request to enable transfer acceleration.
	result, err := client.PutBucketTransferAcceleration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket transfer acceleration %v", err)
	}

	// Display the result.
	log.Printf("put bucket transfer acceleration result:%#v\n", result)
}

Query transfer acceleration status

The following example calls GetBucketTransferAcceleration and prints the Enabled field from the returned TransferAccelerationConfiguration.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a query request for the transfer acceleration status of the bucket.
	request := &oss.GetBucketTransferAccelerationRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Query the transfer acceleration status.
	result, err := client.GetBucketTransferAcceleration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket transfer acceleration %v", err)
	}

	// Display the transfer acceleration status of the bucket.
	log.Printf("get bucket transfer acceleration result:%#v\n", result.TransferAccelerationConfiguration.Enabled)
}

References