Todos os produtos
Search
Central de documentação

Object Storage Service:Configure o pagamento pelo solicitante usando o OSS SDK for Python 1.0

Última atualização: Sep 22, 2026

Ao ativar o pagamento pelo solicitante para um bucket no Object Storage Service (OSS), o sistema cobra as taxas de requisição e tráfego do solicitante em vez do proprietário do bucket. O proprietário do bucket paga apenas as taxas de armazenamento. Ative o pagamento pelo solicitante em um bucket para compartilhar dados sem arcar com as taxas de requisição e tráfego geradas pelo acesso ao seu bucket.

Observações

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para obter mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.

  • Neste tópico, as credenciais de acesso são obtidas a partir de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra como criar uma instância do OSSClient com um endpoint do OSS. Para configurações alternativas, como o uso de um domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization.

  • Para ativar o pagamento pelo solicitante, você deve ter a permissão oss:PutBucketRequestPayment. Para consultar as configurações de pagamento pelo solicitante, você deve ter a permissão oss:GetBucketRequestPayment. Para obter mais informações, consulte Grant a custom policy.

Ative o pagamento pelo solicitante

O código a seguir mostra como ativar o pagamento pelo solicitante para um bucket:

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

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Enable pay-by-requester for the bucket. The default payer is PAYER_BUCKETOWNER. 
result = bucket.put_bucket_request_payment(PAYER_REQUESTER)

print("http respon status: ", result.status)

Consulte as configurações de pagamento pelo solicitante de um bucket

O código a seguir exemplifica como consultar as configurações de pagamento pelo solicitante de um bucket:

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

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Query the pay-by-requester configurations of the bucket. 
result = bucket.get_bucket_request_payment()
print('payer:', result.payer)

Especifique a cobrança de terceiros pelo acesso a objetos

Se você especificar que terceiros paguem pelo acesso a objetos em um bucket, os solicitantes deverão incluir o cabeçalho x-oss-request-payer:requester nas requisições HTTP para executar operações nesses objetos. Se você não incluir esse cabeçalho, o sistema retornará um erro.

O código a seguir demonstra como especificar a cobrança de terceiros ao acessarem objetos por meio das operações PutObject, GetObject e DeleteObject. Use esse mesmo método para especificar a cobrança de terceiros em operações de leitura e escrita de objetos chamando outras operações da API de maneira semelhante.

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

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

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
object_name = 'exampledir/exampleobject.txt'
headers = dict()
headers[OSS_REQUEST_PAYER] = "requester"

# Specify the x-oss-request-payer header in the request to upload the object. 
result = bucket.put_object(object_name, 'test-content', headers=headers)

# Specify the x-oss-request-payer header in the request to download the object. 
result = bucket.get_object(object_name, headers=headers)

# Specify the x-oss-request-payer header in the request to delete the object. 
result = bucket.delete_object(object_name, headers=headers);

Referências

  • Para obter o código de exemplo completo para ativar o pagamento pelo solicitante, visite o GitHub.

  • Para obter mais informações sobre a operação da API para ativar o pagamento pelo solicitante, consulte PutBucketRequestPayment.

  • Para obter mais informações sobre a operação da API para consultar as configurações de pagamento pelo solicitante, consulte GetBucketRequestPayment.