Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Kaynak gruplarını yapılandırma

Son Güncelleme:Oct 02, 2026

Object Storage Service (OSS) paketiniz için bir kaynak grubu belirleyebilir ve OSS SDK for Python kullanarak paketin kaynak grubu kimliğini sorgulayabilirsiniz.

Notlar

  • Bu konudaki örnek kod, Çin (Hangzhou) bölgesi için bölge kimliği olarak varsayılan şekilde cn-hangzhou değerini ve genel bir uç nokta kullanır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden pakete erişmek için dahili uç nokta kullanın. Daha fazla bilgi için bkz. OSS bölgeleri ve uç noktaları.

  • Bir paket için kaynak grubu yapılandırmak amacıyla oss:PutBucketResourceGroup iznine sahip olmanız gerekir. Bir paketin ait olduğu kaynak grubunun kimliğini sorgulamak için oss:GetBucketResourceGroup izninizin bulunması gerekir. Ayrıntılı bilgi edinmek için bkz. Özel ilke atama.

Örnek kod

Bir pakete ait kaynak grubunu yapılandırma

Önemli

Paket için kaynak grubu yapılandırırken bir kaynak grubu kimliği belirtmezseniz paket varsayılan kaynak grubuna atanır. İşlemi gerçekleştirmeden önce hedef kaynak grubunun var olduğundan emin olun. Daha fazla bilgi için bkz. Kaynak grubu oluşturma.

Aşağıdaki örnek kod, bir paket için kaynak grubunun nasıl yapılandırılacağını gösterir:

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.

Bir pakete ait kaynak grubunun kimliğini sorgulama

Aşağıdaki örnek kod, bir paketin kaynak grubu kimliğinin nasıl sorgulanacağını gösterir.

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.

İlgili bilgiler