All Products
Search
Document Center

Object Storage Service:Enable pay-by-requester

Last Updated:Feb 27, 2025

When pay-by-requester is enabled for a bucket, the requester pays the request and traffic fees, and the bucket owner pays only the storage costs. You can enable pay-by-requester for a bucket to share data in the bucket without paying for the request and traffic fees when your bucket is accessed.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the 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 the access credentials, see Configure access credentials.

  • To enable pay-by-requester, you must have the oss:PutBucketRequestPayment permission. To query the pay-by-requester configurations, you must have the oss:GetBucketRequestPayment permission. For more information, see Common examples of RAM policies.

Method

Enable pay-by-requester for a bucket

func (c *Client) PutBucketRequestPayment(ctx context.Context, request *PutBucketRequestPaymentRequest, optFns ...func(*Options)) (*PutBucketRequestPaymentResult, error)

Query the pay-by-requester configurations of a bucket

func (c *Client) GetBucketRequestPayment(ctx context.Context, request *GetBucketRequestPaymentRequest, optFns ...func(*Options)) (*GetBucketRequestPaymentResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request, which can be used to specify the total duration of the request.

request

*PutBucketRequestPaymentRequest

Specifies the parameters of a specific API operation. For more information, see PutBucketRequestPaymentRequest.

*GetBucketRequestPaymentRequest

Specifies the parameters of a specific API operation. For more information, see GetBucketRequestPaymentRequest.

optFns

...func(*Options)

Optional. The operation-level parameter. For more information, see Options.

Response parameters

Response parameter

Type

Description

result

*PutBucketRequestPaymentResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, see PutBucketRequestPaymentResult.

*GetBucketRequestPaymentResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, see GetBucketRequestPaymentResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Examples

Enable pay-by-requester

The following sample code provides an example on how to enable pay-by-requester:

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to specify the payer of the request.
	putRequest := &oss.PutBucketRequestPaymentRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		PaymentConfiguration: &oss.RequestPaymentConfiguration{
			Payer: oss.Requester, // Set the payer of the request to the requester.
		},
	}

	// Send the request to specify the payer of the request.
	putResult, err := client.PutBucketRequestPayment(context.TODO(), putRequest)
	if err != nil {
		log.Fatalf("failed to put bucket request payment %v", err)
	}

	// Display the result of the request.
	log.Printf("put bucket request payment result:%#v\n", putResult)
}

Query the pay-by-requester configurations of a bucket

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

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the pay-by-requester configurations of the bucket.
	getRequest := &oss.GetBucketRequestPaymentRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to query the pay-by-requester configurations of the bucket and process the result.
	getResult, err := client.GetBucketRequestPayment(context.TODO(), getRequest)
	if err != nil {
		log.Fatalf("failed to get bucket request payment %v", err)
	}

	// Display the result of the request.
	log.Printf("get bucket request payment result:%#v\n", getResult)
}

References

  • For the complete sample code that is used to enable pay-by-requester, visit GitHub.

  • For more information about the API operation that you can call to enable pay-by-requester, visit PutBucketRequestPayment.

  • For the complete sample code that is used to query the pay-by-requester configurations of a bucket, visit GitHub.

  • For more information about the API operation that you can call to query the pay-by-requester configurations of a bucket, visit GetBucketRequestPayment.