All Products
Search
Document Center

Object Storage Service:Transfer acceleration using OSS SDK for Go 2.0

Last Updated:Sep 21, 2026

The transfer acceleration feature allows users worldwide to access objects stored in Object Storage Service (OSS) buckets in a short period of time. This feature is applicable to scenarios in which data must be transferred over long geographical distances, and can also be used to download or upload large objects that are gigabytes or terabytes in size.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of China (Hangzhou) as an example. By default, the public endpoint is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mapping between OSS regions and endpoints, see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials (Go SDK V1).

Sample code

Enable transfer acceleration

Below is a code example for enabling transfer acceleration for a 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 the status of transfer acceleration

Below is a code example for querying the transfer acceleration status of a 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 query request for the status of transfer acceleration of the bucket.
	request := &oss.GetBucketTransferAccelerationRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket
	}

	// Perform the query operation.
	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