All Products
Search
Document Center

Object Storage Service:Configure single-connection bandwidth throttling

Last Updated:Sep 11, 2023

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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

Configure bandwidth throttling for simple upload and download

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

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {

	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. 
	// If you do not specify the path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
	fd, err := os.Open("D:\\localpath\\examplefile.txt")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	defer fd.Close()

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	var traffic int64 = 41943040
	// Configure bandwidth throttling for object upload. 
	// Specify the full path of the object. Do not include the bucket name in the full path. 
	err = bucket.PutObject("exampledir/exampleobject.txt", fd, oss.TrafficLimitHeader(traffic))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Configure bandwidth throttling for object download. 
	// Specify the full path of the object. Example: exampledir/exampleobject.txt. Then, specify the full path of the local file. Example: D:\\localpath\\exampleobject.txt. 
	err = bucket.GetObjectToFile("exampledir/exampleobject.txt", "D:\\localpath\\exampleobject.txt", oss.TrafficLimitHeader(traffic))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

Configure bandwidth throttling for multipart upload

The following code provides an example on how to configure single-connection bandwidth throttling for multipart upload:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func main() {

    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
    var traffic int64 = 41943040

    // Upload a large object by using multipart upload. 
    // You can split an object into multiple parts based on the object size. In this example, the object is split into three parts. 
    chunks, err := oss.SplitFileByPartNum("localFile", 3)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Open the object. 
    fd, err := os.Open("fileName")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    defer fd.Close()
    // Initiate a multipart upload task for the object. 
    imur, err := bucket.InitiateMultipartUpload("exampledir/exampleobject.txt")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Upload parts and configure bandwidth throttling. 
    var parts []oss.UploadPart
    for _, chunk := range chunks {
        fd.Seek(chunk.Offset, os.SEEK_SET)
        part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, oss.TrafficLimitHeader(traffic))
        if err != nil {
            fmt.Println("Error:", err)
            os.Exit(-1)
        }
        parts = append(parts, part)
    }
    // Complete the upload. 
    _, err = bucket.CompleteMultipartUpload(imur, parts)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

Configure bandwidth throttling for append upload

The following code provides an example on how to configure single-connection bandwidth throttling for append upload:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

func main() {

    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
    var traffic int64 = 41943040
    // Perform the append upload. 
    // localFileOne and localFileTwo indicates the full paths of the two local files to upload. After localFileOne is uploaded, localFileTwo is uploaded by using append upload. 
    localFileOne := "localFileOne"
    localFileTwo := "localFileTwo"
    fd1, err := os.Open(localFileOne)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    defer fd1.Close()
    fd2, err := os.Open(localFileTwo)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    defer fd2.Close()
    var nextPos int64
    objectName := "exampledir/exampleobject.txt"

    nextPos, err = bucket.AppendObject(objectName, fd1, nextPos, oss.TrafficLimitHeader(traffic))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    nextPos, err = bucket.AppendObject(objectName, fd2, nextPos, oss.TrafficLimitHeader(traffic))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
}

Configure bandwidth throttling for upload and download that use signed URLs

The following sample code provides an example on how to configure single-connection bandwidth throttling when you use signed URLs to upload or download objects:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Use the signed URL to upload the object. 
	// Specify the full path of the local file to upload. Example: D:\\localpath\\exampleobject.txt. 
	fd, err := os.Open("D:\\localpath\\exampleobject.txt")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	defer fd.Close()

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	var traffic int64 = 41943040
	// Obtain the signed URL used to upload the object. 
	// Specify the full path of the object. Do not include the bucket name in the full path. 
	strURL, err := bucket.SignURL("exampledir/exampleobject.txt", oss.HTTPPut, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Upload the local file. 
	err = bucket.PutObjectWithURL(strURL, fd)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Use the signed URL to download the object. 
	// Obtain the signed URL that is used to download the object. 
	strURL, err = bucket.SignURL("exampledir/exampleobject.txt", oss.HTTPGet, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the full path to which you want to download the object. 
	err = bucket.GetObjectToFileWithURL(strURL, "D:\\localpath\\exampleobject.txt")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}