Enable pay-by-requester using OSS SDK for Go 2.0

Updated at:
Copy as MD

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. This is useful for sharing data without incurring additional request and traffic fees.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region and the public endpoint. To access OSS from other Alibaba Cloud services in the same region, 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, 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 request context, which can be used to specify the total duration of the request.

request

*PutBucketRequestPaymentRequest

The request parameters. For more information, see PutBucketRequestPaymentRequest.

*GetBucketRequestPaymentRequest

The request parameters. For more information, see GetBucketRequestPaymentRequest.

optFns

...func(*Options)

Optional. Operation-level parameters. For more information, see Options.

Response parameters

Response parameter

Type

Description

result

*PutBucketRequestPaymentResult

The response. Valid when err is nil. For more information, see PutBucketRequestPaymentResult.

*GetBucketRequestPaymentResult

The response. Valid when err is nil. For more information, see GetBucketRequestPaymentResult.

err

error

The error message. A non-nil value indicates that the request failed.

Examples

Enable pay-by-requester

The following sample code shows 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 the command line arguments.
	flag.Parse()

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

	// Check whether the region is specified.
	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 OSSClient instance.
	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.
	log.Printf("put bucket request payment result:%#v\n", putResult)
}

Query the pay-by-requester configurations of a bucket

The following sample code shows 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 for enabling pay-by-requester, visit GitHub.

  • For the API reference for enabling pay-by-requester, visit PutBucketRequestPayment.

  • For the complete sample code for querying the pay-by-requester configurations of a bucket, visit GitHub.

  • For the API reference for querying pay-by-requester configurations, visit GetBucketRequestPayment.