All Products
Search
Document Center

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

Last Updated:Mar 20, 2026

In pay-by-requester mode, requesters pay the traffic and request fees generated when reading data from a bucket. The bucket owner pays only storage fees. Use this mode when you want to share data without incurring additional traffic and request fees.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The oss:PutBucketRequestPayment permission to configure pay-by-requester mode

  • The oss:GetBucketRequestPayment permission to retrieve the current configuration

  • Access credentials stored as environment variables

For details on granting these permissions, see Grant custom permissions to a RAM user.

Usage notes

  • The examples use the China (Hangzhou) region (cn-hangzhou) with public endpoints. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.

  • The payer parameter accepts two values: "Requester" and "BucketOwner".

Set pay-by-requester mode

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put bucket request payment sample")

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():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    # Set the payer: "Requester" or "BucketOwner"
    result = client.put_bucket_request_payment(oss.PutBucketRequestPaymentRequest(
            bucket=args.bucket,
            request_payment_configuration=oss.RequestPaymentConfiguration(
                payer=args.payer,
            ),
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

if __name__ == "__main__":
    main()

For the complete sample, see put_bucket_request_payment.py.

Get pay-by-requester mode configuration

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="get bucket request payment sample")

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():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    result = client.get_bucket_request_payment(oss.GetBucketRequestPaymentRequest(
            bucket=args.bucket,
    ))

    # result.request_payment_configuration.payer returns "Requester" or "BucketOwner"
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' payer: {getattr(result.request_payment_configuration, "payer", "Not set")},'
          )

if __name__ == "__main__":
    main()

For the complete sample, see get_bucket_request_payment.py.