Tous les produits
Search
Centre de documentation

Object Storage Service:Configurer les groupes de ressources

Dernière mise à jour :Aug 18, 2026

Configurez le groupe de ressources d'un bucket Object Storage Service (OSS) et récupérez son ID à l'aide du SDK OSS pour Python.

Notes

  • Le code exemple de cette rubrique utilise par défaut l'ID de région cn-hangzhou pour la région Chine (Hangzhou) et un endpoint public. Pour accéder au bucket depuis d'autres services Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations, consultez Régions et endpoints OSS.

  • Pour configurer un groupe de ressources pour un bucket, vous devez disposer de l'autorisation oss:PutBucketResourceGroup. Pour récupérer l'ID du groupe de ressources auquel appartient un bucket, vous devez disposer de l'autorisation oss:GetBucketResourceGroup. Pour plus d'informations, consultez Accorder une politique personnalisée.

Code exemple

Configurer le groupe de ressources auquel appartient un bucket

Important

Si vous ne spécifiez pas d'ID de groupe de ressources lors de la configuration d'un bucket, celui-ci est associé au groupe de ressources par défaut. Assurez-vous que le groupe de ressources cible existe avant de poursuivre. Pour plus d'informations, consultez Créer un groupe de ressources.

Le code exemple suivant montre comment configurer un groupe de ressources pour un bucket :

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the purpose of the script: configure the resource group for a bucket.
parser = argparse.ArgumentParser(description="put bucket resource group sample")

# Define the command-line arguments, including the required parameters - region and bucket name - as well as the optional parameters - endpoint and resource group ID.
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('--resource_group_id',
                    help='The ID of the resource group to which the bucket belongs. (Optional, default is an empty string)',
                    default='')

def main():
    # Parse command line arguments to obtain the values entered.
    args = parser.parse_args()

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

    # Use the default configuration to create a cfg object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region attribute of the cfg object to the region provided in the command line.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding settings to initialize the OSSClient instance.
    client = oss.Client(cfg)

    # Send a request to configure the resource group for the specified bucket.
    result = client.put_bucket_resource_group(oss.PutBucketResourceGroupRequest(
            bucket=args.bucket,  # Name of the bucket.
            bucket_resource_group_configuration=oss.BucketResourceGroupConfiguration(
                resource_group_id=args.resource_group_id,  # Resource group ID.
            ),
    ))

    # Display the HTTP status code of the operation and request ID to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main()  # Entry point of the script. The control flow starts here.

Récupérer l'ID du groupe de ressources auquel appartient un bucket

Le code exemple suivant montre comment récupérer l'ID du groupe de ressources d'un bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the purpose of the script. The example describes how to query the resource group configurations of a bucket.
parser = argparse.ArgumentParser(description="get bucket resource group sample")

# Define the command-line arguments, including the required parameters — region and bucket name — as well as the optional endpoint parameter.
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():
    # Parse the command line arguments to obtain the values specified by the user.
    args = parser.parse_args()

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

    # Use the default configuration to create a cfg object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region attribute of the cfg object to the region provided in the command line.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding settings to initialize the OSS Client instance.
    client = oss.Client(cfg)

    # Send a request to query the resource group of the bucket.
    result = client.get_bucket_resource_group(oss.GetBucketResourceGroupRequest(
            bucket=args.bucket,  # Name of the bucket.
    ))

    # Display the HTTP status code, request ID, and resource group ID to check the request status and details of configuration.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' resource group id: {getattr(result.bucket_resource_group_configuration, "resource_group_id", "Not set")},'
          )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main()  # Entry point of the script. The control flow starts here.

Références