When pay-by-requester is enabled for a bucket in Object Storage Service (OSS), the requester is charged the requests and traffic fees instead of the bucket owner. The bucket owner is charged only the storage fees. You can enable pay-by-requester for a bucket to share data in the bucket without paying for the request and traffic fees incurred by the access to your bucket.

Enable pay-by-requester

The following code provides an example on how to enable pay-by-requester for a bucket:

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 the 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)
  }

  // Initialize the pay-by-requester mode. 
  reqPayConf := oss.RequestPaymentConfiguration{
    Payer: "Requester",
  }

  // Enable pay-by-requester for the bucket. 
  err = client.SetBucketRequestPayment("<yourBucketName>", reqPayConf)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}

Query pay-by-requester configurations

The following code provides an example on how to query the pay-by-requester configurations of a bucket:

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 the 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)
    }

  // Query the pay-by-requester configurations of the bucket. 
  ret, err := client.GetBucketRequestPayment("yourBucketName")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Specify that the pay-by-requester configurations of the bucket are displayed. 
  fmt.Println("Bucket request payer:", ret.Payer)
}

Specify that third parties are charged for access to objects

If you specify that third parties are charged for access to objects in a bucket, requesters must include the x-oss-request-payer:requester header in the HTTP requests to perform operations on your objects. If this header is not included, an error is returned.

The following code provides an example on how to specify that third parties are charged when they access objects by calling the PutObject, GetObject, and DeleteObject operations. You can use the method to specify that third parties are charged when they perform read and write operations on objects by calling other API operations in the similar way.

package main

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

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 the actual endpoint. 
  // Set yourAccessKeyId and yourAccessKeySecret to the AccessKey pair of the third-party. 
  payerClient, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("New Error:", err)
    os.Exit(-1)
  }
  
  // Specify the bucket name. 
  payerBucket, err := payerClient.Bucket("examplebucket")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
    
  // If the bucket owner enables the pay-by-requester mode, requesters must include the oss.RequestPayer(oss.Requester) parameter in the requests to access the authorized content. 
  // If the bucket owner does not enable the pay-by-requester mode, requesters do not need to include the oss.RequestPayer(oss.Requester) parameter in the requests to access the authorized content. 

  // Upload an object. 
  // Specify the full path of the object. The path cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
  key := "exampledir/exampleobject.txt"
  err = payerBucket.PutObject(key, strings.NewReader("objectValue"), oss.RequestPayer("requester"))
  if err != nil {
    fmt.Println("put Error:", err)
    os.Exit(-1)
  }

  // List all objects in the bucket.   
  lor, err := payerBucket.ListObjects(oss.RequestPayer(oss.Requester))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Specify that the names of objects in the bucket are displayed. 
  for _, l := range lor.Objects {
    fmt.Println("the Key name is :", l.Key)
  }

  // Download the object. 
  body, err := payerBucket.GetObject(key, oss.RequestPayer(oss.Requester))
  if err != nil {
    fmt.Println("Get Error:", err)
    os.Exit(-1)
  }
  // You must close the stream obtained by using the GetObject API operation after the object is read. Otherwise, connection leaks may occur. Consequently, no connections are available and your application cannot work. 
  defer body.Close()

  // Read and display the obtained content. 
  data, err := ioutil.ReadAll(body)
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  fmt.Println("data:", string(data))

  // Delete the object. 
  err = payerBucket.DeleteObject(key, oss.RequestPayer(oss.Requester))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
}

References

  • For more information about the complete sample code that is used to enable pay-by-requester for a bucket, visit GitHub.
  • For more information about the API operation that you can call to enable pay-by-requester for a bucket, see PutBucketRequestPayment.
  • For more information about the API operation that you can call to query the pay-by-requester configurations of a bucket, see GetBucketRequestPayment.