All Products
Search
Document Center

Object Storage Service:Pay-by-requester mode (OSS SDK for Python V2)

Last Updated:Jul 31, 2025

In pay-by-requester mode, requesters pay the traffic and request fees that are generated when they read data from a bucket. The bucket owner pays only the storage fees. You can enable this feature if you want to share data without incurring additional traffic and request fees.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region as an example. The region ID is cn-hangzhou. Public endpoints are used by default. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use internal endpoints. For more information about the regions and endpoints that are supported by OSS, see OSS regions and endpoints.

  • To set the pay-by-requester mode, you must have the oss:PutBucketRequestPayment permission. To retrieve the pay-by-requester mode configuration, you must have the oss:GetBucketRequestPayment permission. For more information, see Grant custom permissions to a RAM user.

Sample code

Set the pay-by-requester mode

You can use the following code to set the pay-by-requester mode.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: set the request payment configuration for a bucket.
parser = argparse.ArgumentParser(description="put bucket request payment sample")

# Define command-line arguments, including the required region, bucket name, optional endpoint, and payer.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--payer', help='Indicates who pays the download and request fees. Possible values: "Requester" or "BucketOwner"', required=True)

def main():
    # Parse command-line arguments to obtain user-input values.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSS client and prepare for interaction with OSS.
    client = oss.Client(cfg)

    # Send a request to set the request payment configuration for the specified bucket.
    result = client.put_bucket_request_payment(oss.PutBucketRequestPaymentRequest(
            bucket=args.bucket,  # The bucket name.
            request_payment_configuration=oss.RequestPaymentConfiguration(
                payer=args.payer,  # The payer. Valid values: "Requester" or "BucketOwner".
            ),
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

Obtain the pay-by-requester mode configuration

You can use the following code to retrieve the pay-by-requester mode configuration.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: obtain the request payment configuration for a bucket.
parser = argparse.ArgumentParser(description="get bucket request payment sample")

# Define command-line arguments, including the required region, bucket name, and optional endpoint.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse command-line arguments to obtain user-input values.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSS client and prepare for interaction with OSS.
    client = oss.Client(cfg)

    # Send a request to obtain the request payment configuration for the specified bucket.
    result = client.get_bucket_request_payment(oss.GetBucketRequestPaymentRequest(
            bucket=args.bucket,  # The bucket name.
    ))

    # Print the status code, request ID, and payer information of the operation result to confirm the request status and configuration details.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' payer: {getattr(result.request_payment_configuration, "payer", "Not set")},'
          )

# When this script is directly executed, call the main function to start processing the logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

References