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.

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

func main() {  

  // 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. 
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. 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 bucket name. 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. 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 bandwidth throttling for object download. 
  // Specify the full paths of the object and the local file. The full path of the object cannot contain the bucket name. Example of the object path: exampledir/exampleobject.txt. Example of the local file path: 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() {

    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. 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 bucket name. 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. 
    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 multipart 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() {

    // 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. 
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. 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 bucket name. 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 operation. 
    // 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"
  "os"
  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // 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. 
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. 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 bucket name. 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()

  // 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 that is 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 a 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. After the object is downloaded to the specified path, the downloaded object is saved as a local file. 
  err = bucket.GetObjectToFileWithURL(strURL, "D:\\localpath\\exampleobject.txt")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}