All Products
Search
Document Center

Object Storage Service:Configure pay-by-requester using OSS SDK for Python 1.0

Last Updated:Mar 20, 2026

When pay-by-requester is enabled on a bucket, requesters pay the request and traffic fees instead of the bucket owner. The bucket owner pays only storage fees. This is useful when you want to share large datasets — such as reference data, geospatial information, or public archives — without absorbing the access costs generated by others.

Prerequisites

Before you begin, make sure you have:

  • An OSS bucket

  • The oss:PutBucketRequestPayment permission to enable pay-by-requester

  • The oss:GetBucketRequestPayment permission to query the pay-by-requester configuration

Usage notes

  • Examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see OSS regions and endpoints.

  • Access credentials in this topic are read from environment variables. For setup instructions, see Configure access credentials using OSS SDK for Python 1.0.

  • The examples create an OSSClient instance using an OSS endpoint. To create one using a custom domain name or Security Token Service (STS), see Initialization.

Enable pay-by-requester

Call put_bucket_request_payment with PAYER_REQUESTER to shift request and traffic charges to requesters. The default payer is PAYER_BUCKETOWNER.

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import PAYER_BUCKETOWNER, PAYER_REQUESTER

# Read access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Replace with the endpoint for your bucket's region.
# Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Region ID corresponding to the endpoint. Required for signature algorithm V4.
region = "cn-hangzhou"

# Replace yourBucketName with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Enable pay-by-requester. Requesters will pay request and traffic fees.
result = bucket.put_bucket_request_payment(PAYER_REQUESTER)
print("HTTP response status:", result.status)

Query the pay-by-requester configuration

Call get_bucket_request_payment to check the current payer setting.

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Read access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Region ID corresponding to the endpoint. Required for signature algorithm V4.
region = "cn-hangzhou"

# Replace yourBucketName with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Query the payer configuration.
result = bucket.get_bucket_request_payment()
print("Payer:", result.payer)

Access objects as a third-party requester

When pay-by-requester is enabled, all requesters must include the x-oss-request-payer: requester HTTP header in every request. Requests without this header return an error.

The examples below use the OSS_REQUEST_PAYER constant from oss2.headers to set the required header. The same pattern applies to any other OSS API operations.

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.headers import OSS_REQUEST_PAYER

# Read access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Region ID corresponding to the endpoint. Required for signature algorithm V4.
region = "cn-hangzhou"

# Replace yourBucketName with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Replace with the full object path, excluding the bucket name.
# Example: exampledir/exampleobject.txt
object_name = 'exampledir/exampleobject.txt'

# Set the x-oss-request-payer header to declare the requester as the payer.
headers = {OSS_REQUEST_PAYER: "requester"}

# Upload an object.
result = bucket.put_object(object_name, 'test-content', headers=headers)

# Download an object.
result = bucket.get_object(object_name, headers=headers)

# Delete an object.
result = bucket.delete_object(object_name, headers=headers)

What's next