Todos os produtos
Search
Central de documentação

Object Storage Service:Criar buckets com o OSS SDK for Python 2.0

Última atualização: Jul 03, 2026

Um bucket é um contêiner de objetos. Saiba como criar um bucket com o OSS SDK for Python 2.0.

Permissões

Por padrão, uma conta Alibaba Cloud tem permissões totais. Usuários RAM ou funções RAM vinculados a essa conta não têm permissão inicialmente. A conta Alibaba Cloud ou o administrador da conta deve conceder as permissões de operação por meio de políticas do RAM ou Bucket Policy.

API

Ação

Descrição

PutBucket

oss:PutBucket

Cria um bucket.

oss:PutBucketAcl

Após criar o bucket, você precisa dessa permissão para modificar a ACL do bucket.

Observações

  • O código de exemplo usa o ID de região cn-hangzhou (China (Hangzhou)). O endpoint público é usado por padrão. Para acessar recursos de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints compatíveis, consulte Regiões e endpoints.

Método

put_bucket(request: PutBucketRequest, **kwargs) → PutBucketResult

Parâmetro da solicitação

Parâmetro

Tipo

Descrição

request

PutBucketRequest

O parâmetro da solicitação. PutBucketRequest.

Parâmetro de resposta

Tipo

Descrição

PutBucketResult

A resposta. PutBucketResult.

Para ver a API completa, consulte put_bucket.

Código de exemplo

O código de exemplo a seguir cria um bucket com a classe de armazenamento Standard.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser.
parser = argparse.ArgumentParser(description="put bucket sample")
# Specify the required command line parameter --region, which specifies the region in which the bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the required command line parameter --bucket, which specifies the name of the bucket. 
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the optional command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    args = parser.parse_args()  # Parse command line parameters.

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

    # Load the default configurations of the SDK and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    # Specify the region in which the bucket is located.
    cfg.region = args.region
    # If the endpoint parameter is provided, specify the endpoint that other services can use to access OSS.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the configurations to create an OSSClient instance.
    client = oss.Client(cfg)

    # Execute the request to create a bucket and set its storage class to Standard.
    result = client.put_bucket(oss.PutBucketRequest(
        bucket=args.bucket,
        create_bucket_configuration=oss.CreateBucketConfiguration(
            storage_class='Standard'
        )
    ))
    # Output the HTTP status code in the response and the request ID used to check whether the request is successful.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()  # Entry point of the script. The main function is invoked when the file is run directly.

Referências