This topic describes how to ensure other applications have sufficient bandwidth when you perform upload and download operations on OSS. You can include the x-oss-traffic-limit parameter in your requests and set bandwidth limits.

Note For more information about the scenarios and precautions of the single-connection bandwidth throttling feature, see Single-connection bandwidth throttling in the OSS Developer Guide.

Configure bandwidth throttling for common uploads and downloads

Use the following code to configure bandwidth throttling when you upload and download objects to and from OSS:

package main

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

func main() {
  //
  // You can use the single-connection bandwidth throttling feature when you call the following operations:
  // PutObject/AppendObject/PostObject/CopyObject/UploadPart/UploadPartCopy
  // The following code provides examples of how to configure bandwidth throttling when you call these operations.
  //

  // Create an OSSClient instance.
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Obtain the bucket.
  bucket, err := client.Bucket("<yourBucketName>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Specify the object to be uploaded.
  fd, err := os.Open("<yourLocalFile>")
  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. 
  // 41943040 indicates that the bandwidth limit for the request is 40 Mbit/s, or 5 MB/s.
  var traffic int64 = 41943040
  // Example 1: Configure bandwidth throttling for the object to be uploaded.
  err = bucket.PutObject("<youObjectName>", fd, oss.TrafficLimitHeader(traffic))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Example 2: Configure bandwidth throttling for the object to be downloaded. newFile indicates the name of the downloaded object.
  err = bucket.GetObjectToFile("<youObjectName>", "newFile", oss.TrafficLimitHeader(traffic))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Example 3: Configure bandwidth throttling for append upload. In this case, after localFileOne is uploaded, you can append localFileTwo .
  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("<yourObjectName>", fd2, nextPos, oss.TrafficLimitHeader(traffic))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Example 4: Configure bandwidth throttling for multipart upload of a large object.
  // You can split an object 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 bandwidth throttling.
  imur, err := bucket.InitiateMultipartUpload("<yourObjectName>")
  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)
  }
}

Configure bandwidth throttling for uploads and downloads that use signed URLs

Use the following code to configure bandwidth throttling when you upload and download objects from and to OSS by using signed URLs.

package main

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

func main() {
  // Create an OSSClient instance.
  client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Obtain the bucket.
  bucket, err := client.Bucket("<yourBucketName>")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

  // Example 1: Use the signed URL to upload an object.
  // 
  // Open the object.
  fd, err := os.Open("<localfile>")
  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.
  // 41943040 indicates that the bandwidth limit for the request is 40 Mbit/s, or 5 MB/s.
  var traffic int64 = 41943040
  // Obtain the signed URL for uploading an object.
  strURL, err := bucket.SignURL("<yourObjectName>", oss.HTTPPut, 60, oss.TrafficLimitParam(traffic))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }

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

  // Example 2: Use the signed URL to download an object.
  // Obtain the signed URL for downloading an object.
  strURL, err = bucket.SignURL("<yourObjectName>", oss.HTTPGet, 60, oss.TrafficLimitParam(traffic))
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Download the object. newFile indicates the name of the downloaded object.
  err = bucket.GetObjectToFileWithURL(strURL, "newFile")
  if err ! = nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}