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.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, 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.
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).
To enable pay-by-requester, you must have the
oss:PutBucketRequestPaymentpermission. To query the pay-by-requester configurations, you must have theoss:GetBucketRequestPaymentpermission. For more information, see Attach a custom policy to a RAM user.
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() {
// Obtain access credentials from environment variables.
// Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the endpoint to the region of your bucket, for example, https://oss-cn-hangzhou.aliyuncs.com.
// Set the region to match, for example, cn-hangzhou.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature algorithm version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("New Error:", err)
os.Exit(-1)
}
// Configure pay-by-requester.
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 the pay-by-requester configurations of a bucket
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() {
// Obtain access credentials from environment variables.
// Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the endpoint to the region of your bucket, for example, https://oss-cn-hangzhou.aliyuncs.com.
// Set the region to match, for example, cn-hangzhou.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature algorithm version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("New Error:", err)
os.Exit(-1)
}
// Query the pay-by-requester configurations.
ret, err := client.GetBucketRequestPayment("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Display the pay-by-requester configurations.
fmt.Println("Bucket request payer:", ret.Payer)
}
Set third-party requesters as payers for object access
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"
"io"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables.
// Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set the endpoint to the region of your bucket, for example, https://oss-cn-hangzhou.aliyuncs.com.
// Set the region to match, for example, cn-hangzhou.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature algorithm version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
payerClient, err := oss.New("yourEndpoint", "", "", clientOptions...)
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)
}
// When pay-by-requester is enabled, third-party requesters must pass oss.RequestPayer(oss.Requester).
// When pay-by-requester is not enabled, this parameter is not required.
// Upload an object.
// Specify the full object path. Do not include the bucket name.
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 objects in the bucket.
lor, err := payerBucket.ListObjects(oss.RequestPayer(oss.Requester))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Print object keys.
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)
}
// Close the stream after reading to prevent connection leaks.
defer body.Close()
// Read and display the content.
data, err := io.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 the complete sample code, visit GitHub.
For more information about the API operation that you can call to enable pay-by-requester, see PutBucketRequestPayment.
For more information about the API operation that you can call to query the pay-by-requester configurations, see GetBucketRequestPayment.