Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Python 2.0 için OSS SDK ile paket ilkelerini yönetme

Son Güncelleme:Oct 02, 2026

Paket ilkeleri, anonim kullanıcıların veya Alibaba Cloud hesapları, RAM kullanıcıları ve RAM rolleri gibi tanımlanmış kullanıcıların belirli OSS kaynaklarına erişimini yetkilendirmenize veya kısıtlamanıza olanak tanır. Örneğin, başka bir Alibaba Cloud hesabındaki bir RAM kullanıcısına belirli OSS kaynakları üzerinde salt okunur izinler verebilirsiniz.

Notlar

  • Paket ilkelerini yapılandırmadan önce bu özelliği anladığınızdan emin olun.

  • Bu konudaki örnek kod, Çin (Hangzhou) bölgesinin bölge kimliği olan cn-hangzhou değerini kullanır. Varsayılan olarak, bir paketteki kaynaklara erişmek için genel uç nokta kullanılır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden paketteki kaynaklara erişmek istiyorsanız dahili bir uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bir paket ilkesi yapılandırmak için oss:PutBucketPolicy izni gerekir. Bir paket ilkesini sorgulamak için oss:GetBucketPolicy izni gerekir. Bir paket ilkesini silmek için oss:DeleteBucketPolicy izni gerekir. Daha fazla bilgi için bkz. Özel bir ilke atama.

Yöntemler

Paket ilkesi yapılandırma

put_bucket_policy(request: PutBucketPolicyRequest, **kwargs) → PutBucketPolicyResult

Paket ilkelerini sorgulama

get_bucket_policy(request: GetBucketPolicyRequest, **kwargs) → GetBucketPolicyResult

Paket ilkesi silme

delete_bucket_policy(request: DeleteBucketPolicyRequest, **kwargs) → DeleteBucketPolicyResult

Parametre

Tür

Açıklama

request

PutBucketPolicyRequest

PutBucketPolicy işlemi için istek. İstek parametreleri için bkz. PutBucketPolicyRequest.

GetBucketPolicyRequest

GetBucketPolicy işlemi için istek. İstek parametreleri için bkz. GetBucketPolicyRequest.

DeleteBucketPolicyRequest

DeleteBucketPolicy işlemi için istek. İstek parametreleri için bkz. DeleteBucketPolicyRequest.

Yanıt parametreleri


Tür


Açıklama


PutBucketPolicyResult


Dönüş değeri. Daha fazla bilgi için bkz. PutBucketPolicyResult.


GetBucketPolicyResult


Dönüş değeri. Daha fazla bilgi için bkz. GetBucketPolicyResult.


DeleteBucketPolicyResult


Dönüş değeri. Daha fazla bilgi için bkz. DeleteBucketPolicyResult.

Bir paket ilkesi yapılandırmaya yönelik eksiksiz yöntem tanımı için bkz. put_bucket_policy.

Paket ilkelerini sorgulamaya yönelik eksiksiz yöntem tanımı için bkz. get_bucket_policy.

Bir paket ilkesini silmeye yönelik eksiksiz yöntem tanımı için bkz. delete_bucket_policy.

Örnek kod

Paket ilkesi yapılandırma

Aşağıdaki örnek kod bir paket ilkesi yapılandırır.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser for obtaining the values of the command-line parameters.
parser = argparse.ArgumentParser(description="put bucket policy sample")
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 parameters.
    args = parser.parse_args()

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

    # Use the default configuration of the SDK.
    cfg = oss.config.load_default()
    # Set the credential provider.
    cfg.credentials_provider = credentials_provider
    # Specify the region.
    cfg.region = args.region
    # If an endpoint is provided from the command line, update the endpoint in the configuration with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # In the following example, the bucket owner whose UID is 174649585760xxxx uses a bucket policy to authorize a RAM user whose UID is 20214760404935xxxx to list all objects in examplebucket.
    policy_text = "{\"Statement\": [{\"Effect\": \"Allow\", \"Action\": [\"oss:GetObject\", \"oss:ListObjects\"], \"Principal\": [\"20214760404935xxxx\"], \"Resource\": [\"acs:oss:*:174649585760xxxx:examplebucket/*\"]}], \"Version\": \"1\"}"

    # Apply the policy to the specified bucket.
    result = client.put_bucket_policy(oss.PutBucketPolicyRequest(
            bucket=args.bucket,
            body=policy_text,
    ))

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

if __name__ == "__main__":
    main()

Paket ilkelerini sorgulama

Aşağıdaki örnek kod paket ilkelerini sorgular.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser.
parser = argparse.ArgumentParser(description="get bucket policy sample")
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 parameters.
    args = parser.parse_args()

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

    # Use the default configuration of the SDK.
    cfg = oss.config.load_default()
    # Set the credential provider.
    cfg.credentials_provider = credentials_provider
    # Specify the region.
    cfg.region = args.region
    # If an endpoint is provided from the command line, update the endpoint in the configuration with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Call get_bucket_policy to query the bucket policies.
    result = client.get_bucket_policy(oss.GetBucketPolicyRequest(
            bucket=args.bucket,
    ))

    # Display the HTTP status code, request ID, and response body.
    print(f'status code: {result.status_code},'
            f' request id: {result.request_id},'
            f' body: {result.body},'
    )

# Call the main function when the script is directly run.
if __name__ == "__main__":
    main()

Paket ilkesi silme

Aşağıdaki örnek kod bir paket ilkesini siler.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line parameter parser.
parser = argparse.ArgumentParser(description="delete bucket policy sample")
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 command-line parameters.
    args = parser.parse_args()

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

    # Use the default configuration of the SDK.
    cfg = oss.config.load_default()
    # Set the credential provider.
    cfg.credentials_provider = credentials_provider
    # Specify the region.
    cfg.region = args.region
    # If an endpoint is provided from the command line, update the endpoint in the configuration with the provided endpoint.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Call the delete_bucket_policy method.
    result = client.delete_bucket_policy(oss.DeleteBucketPolicyRequest(
            bucket=args.bucket,  # Specify the name of the bucket to perform the operation on.
    ))

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

# Execute the main function when the script is directly run.
if __name__ == "__main__":
    main()

İlgili Konular

  • Bir paket ilkesi yapılandırmaya yönelik eksiksiz örnek kod için put_bucket_policy.py adresini ziyaret edin.

  • Paket ilkelerini sorgulamaya yönelik eksiksiz örnek kod için get_bucket_policy.py adresini ziyaret edin.

  • Bir paket ilkesini silmeye yönelik eksiksiz örnek kod için delete_bucket_policy.py adresini ziyaret edin.