All Products
Search
Document Center

Object Storage Service:Single-connection bandwidth throttling (Go SDK V1)

Last Updated:Nov 28, 2025

This topic describes how to add parameters in an object upload or download request to set the limit of upload or download bandwidth. This ensures sufficient bandwidth for other applications.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

Sample code

The following sample code provides an example on how to configure single-connection bandwidth throttling for simple upload and download:

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket's region. For example, for the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, use cn-hangzhou. For other regions, use the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Set the signature version.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Set the full path of the local file to upload. For example, D:\\localpath\\examplefile.txt.
	// If you do not specify a local path, the file is uploaded from the project's default local path.
	localFilePath := "D:\\localpath\\examplefile.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Set the upload speed limit. The value is a number, and the default unit is bit/s. This example sets the limit to 5 MB/s.
	var traffic int64 = 41943040

	// Upload the file with bandwidth throttling.
	// Set the full path of the object. The full path cannot contain the bucket name.
	objectName := "exampledir/exampleobject.txt"
	err = bucket.PutObject(objectName, fd, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	// Download the file with bandwidth throttling.
	// Set the full path of the object (for example, exampledir/exampleobject.txt) and the full path of the local file (for example, D:\\localpath\\exampleobject.txt). The full path of the object cannot contain the bucket name.
	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFile(objectName, downloadFilePath, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

Scenarios

Set single-connection bandwidth throttling for multipart uploads

The following code provides an example of how to set single-connection bandwidth throttling for a multipart upload.

package main

import (
	"io"
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket's region. For example, for the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, use cn-hangzhou. For other regions, use the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Set the signature version.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Set the upload speed limit. The value is a number, and the default unit is bit/s. This example sets the limit to 5 MB/s.
	traffic := int64(41943040)

	// Upload a large file using multipart upload.
	// Split the file into three parts. You can set the number of parts based on the file size.
	localFilePath := "localFile"
	chunks, err := oss.SplitFileByPartNum(localFilePath, 3)
	if err != nil {
		log.Fatalf("Failed to split file '%s': %v", localFilePath, err)
	}

	// Open the file.
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Initialize the file for upload.
	objectName := "exampledir/exampleobject.txt"
	imur, err := bucket.InitiateMultipartUpload(objectName)
	if err != nil {
		log.Fatalf("Failed to initiate multipart upload for '%s': %v", objectName, err)
	}

	// Upload parts with bandwidth throttling.
	var parts []oss.UploadPart
	for _, chunk := range chunks {
		_, err := fd.Seek(chunk.Offset, io.SeekStart)
		if err != nil {
			log.Fatalf("Failed to seek to offset %d in file '%s': %v", chunk.Offset, localFilePath, err)
		}
		part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, oss.TrafficLimitHeader(traffic))
		if err != nil {
			log.Fatalf("Failed to upload part %d of '%s': %v", chunk.Number, objectName, err)
		}
		parts = append(parts, part)
	}

	// Complete the upload.
	_, err = bucket.CompleteMultipartUpload(imur, parts)
	if err != nil {
		log.Fatalf("Failed to complete multipart upload for '%s': %v", objectName, err)
	}

	log.Println("Multipart upload completed successfully")
}

Set single-connection bandwidth throttling for append uploads

The following code provides an example of how to set single-connection bandwidth throttling for an append upload.

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket's region. For example, for the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, use cn-hangzhou. For other regions, use the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Set the signature version.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Set the upload speed limit. The value is a number, and the default unit is bit/s. This example sets the limit to 5 MB/s.
	traffic := int64(41943040)

	// Append the upload.
	// localFileOne and localFileTwo are the full paths of the two local files to upload. After localFileOne is uploaded, localFileTwo is appended to it.
	localFileOne := "localFileOne"
	localFileTwo := "localFileTwo"

	// Open the first file.
	fd1, err := os.Open(localFileOne)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFileOne, err)
	}
	defer fd1.Close()

	// Open the second file.
	fd2, err := os.Open(localFileTwo)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFileTwo, err)
	}
	defer fd2.Close()

	// Define the full path of the object.
	objectName := "exampledir/exampleobject.txt"

	// Initialize nextPos.
	var nextPos int64

	// Append the first file.
	nextPos, err = bucket.AppendObject(objectName, fd1, nextPos, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to append object '%s' from file '%s': %v", objectName, localFileOne, err)
	}

	// Append the second file.
	nextPos, err = bucket.AppendObject(objectName, fd2, nextPos, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to append object '%s' from file '%s': %v", objectName, localFileTwo, err)
	}

	log.Println("Append upload completed successfully")
}

Set single-connection bandwidth throttling for uploads and downloads that use signed URLs

The following code provides an example of how to set single-connection bandwidth throttling when you use signed URLs to upload and download files.

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket's region. For example, for the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, use cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Set the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Upload the file using a signed URL.
	// Set the full path of the local file. For example, D:\\localpath\\exampleobject.txt.
	localFilePath := "D:\\localpath\\exampleobject.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Set the upload speed limit. The value is a number, and the default unit is bit/s. This example sets the limit to 5 MB/s.
	traffic := int64(41943040)

	// Get the URL for uploading the file.
	// Set the full path of the object. The full path cannot contain the bucket name.
	objectName := "exampledir/exampleobject.txt"
	strURL, err := bucket.SignURL(objectName, oss.HTTPPut, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for uploading '%s': %v", objectName, err)
	}

	// Upload the local file.
	err = bucket.PutObjectWithURL(strURL, fd)
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	// Download the file using a signed URL.
	// Get the URL for downloading the file.
	strURL, err = bucket.SignURL(objectName, oss.HTTPGet, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for downloading '%s': %v", objectName, err)
	}

	// Set the full path where the object is downloaded to a local file.
	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFileWithURL(strURL, downloadFilePath)
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

References

  • For more information about the API operation for uploading files, see PutObject.

  • For more information about the API operation for downloading files, see GetObjectToFile.