This topic describes how to add parameters in an object upload and download request to set the limit of upload or download bandwidth. This way, you can ensure other applications have sufficient bandwidth.

Configure single-connection bandwidth throttling for simple upload and download

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

package main

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

func main() {  

  // Create an OSSClient instance. 
  // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")  
  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 to upload. Example: D:\\localpath\\examplefile.txt. 
  // By default, if the path of the local file is not specified, 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()

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

  // Configure single-connection bandwidth throttling for the 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)
  }

  // Upload objects in append upload.
  // localFileOne and localFileTwo indicates the full paths of the two local files to upload. After localFileOne is uploaded, localFileTwo is uploaded in append upload. 
  localFileOne := "localFileOne"
  localFileTwo := "localFileTwo"
  fd1, err := os.Open(localFileOne)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  defer fd.Close()
  fd2, err := os.Open(localFileTwo)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  defer fd.Close()

  var nextPos int64
  nextPos, err = bucket.AppendObject("<yourObjectName>", fd1, nextPos, oss.TrafficLimitHeader(traffic))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  nextPos, err = bucket.AppendObject("exampledir/exampleobject.txt", fd2, nextPos, oss.TrafficLimitHeader(traffic))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Upload a large local file in multipart upload.
  // You can split the local file into multiple parts based on the object size. 
  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()
  // Initialize the multipart upload and configure single-connection bandwidth throttling. 
  imur, err := bucket.InitiateMultipartUpload("exampledir/exampleobject.txt")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Upload parts and set the bandwidth limit. 
  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 multipart upload. 
  _, err = bucket.CompleteMultipartUpload(imur, parts)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}Part

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

The following 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"
  "os"
  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret") 
  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 an 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()

  // Set the upload bandwidth limit. The value of the parameter must be a number. The value must be expressed in bit/s. In this example, the upload 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. The full path of the object cannot contain the bucket name. 
  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)
  }

  // Generate a signed URL used to download an object. 
  // Obtain the signed URL 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 of the local file to which you want the object to be downloaded. 
  err = bucket.GetObjectToFileWithURL(strURL, "D:\\localpath\\exampleobject.txt")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}