Cuando se habilita el pago por solicitante en un bucket de Object Storage Service (OSS), los cargos por solicitudes y tráfico se facturan a quien realiza la solicitud, no al propietario del bucket. El propietario solo paga las tarifas de almacenamiento. Esta función le permite compartir datos sin asumir los costos de solicitud y tráfico que genera el acceso a su bucket.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si accede a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región, use un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Las credenciales de acceso se obtienen de variables de entorno. Para conocer cómo configurar estas credenciales, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Para habilitar el pago por solicitante, necesita el permiso
oss:PutBucketRequestPayment. Para consultar la configuración de pago por solicitante, requiere el permisooss:GetBucketRequestPayment. Para obtener más información, consulte Grant a custom policy.
Habilitar el pago por solicitante
A continuación se muestra un ejemplo de código para habilitar el pago por solicitante en un 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)
Consultar la configuración de pago por solicitante de un bucket
El siguiente ejemplo de código muestra cómo consultar la configuración de pago por solicitante de un 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)
Especificar que terceros paguen por el acceso a los objetos
Si establece que terceros asuman el costo de acceso a los objetos de un bucket, los solicitantes deben incluir el encabezado x-oss-request-payer:requester en las solicitudes HTTP para ejecutar operaciones sobre dichos objetos. De lo contrario, se devuelve un error.
El siguiente ejemplo de código muestra cómo indicar que terceros pagan al acceder a los objetos mediante las operaciones PutObject, GetObject y DeleteObject. Este método también aplica para otras operaciones de API de lectura y escritura de objetos.
# -*- 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);
Referencias
Para consultar el código de ejemplo completo sobre cómo habilitar el pago por solicitante, visite GitHub.
Para obtener más información sobre la operación de API que permite habilitar el pago por solicitante, consulte PutBucketRequestPayment.
Para conocer los detalles de la operación de API que sirve para consultar la configuración de pago por solicitante, consulte GetBucketRequestPayment.