このトピックでは、OSS Python SDK を初期化する方法について説明します。
OSS Python SDK のほとんどの操作は oss2.Service および oss2.Bucket を介して実行されます。
- oss2.Service クラスは、バケットを一覧表示するために使用されます。
- oss2.Bucket クラスは、オブジェクトのアップロード、ダウンロード、削除、そしてバケットの設定に使用されます。
2 つのクラスを初期化するには、エンドポイントを指定しなければなりません。 oss2.Service クラスは、カスタムドメイン (CNAME) とのアクセスをサポートしていません。 エンドポイントの詳細については、「リージョンおよびエンドポイント」および「カスタムドメイン名のバインド方法」をご参照ください。
oss2.Service クラスの初期化
詳しくは、「バケットの管理」のバケット一覧をご参照ください。
oss2.Bucket クラスの初期化
- OSS ドメイン名を使用してクラスを初期化
次のコードを実行して、OSS ドメイン名を使用して oss2.Bucket クラスを初期化します。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This case uses the Hangzhou endpoint as an example. Fill in the region endpoint name according to the actual circumstances. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' bucket = oss2.Bucket(auth, endpoint, '<yourBucketName>')
- カスタムドメイン名を使用してクラスを初期化
次のコードを実行して、カスタムドメイン名を使用して oss2.Bucket クラスを初期化します。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # Use a custom domain name: my-domain.com as an example. is_cname=True indicates that the CNAME is enabled. Cname means binding a custom domain name to the storage space. CNAME indicates a custom domain bound to a bucket. cname = 'http://my-domain.com' bucket = oss2.Bucket(auth, cname, '<yourBucketName>', is_cname=True)
- 接続タイムアウトの設定
次のコードを実行して、接続タイムアウトを設定します。
# -*- coding: utf-8 -*- import oss2 // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # Set the connection timeout to 30 seconds. bucket = oss2.Bucket(auth, endpoint, '<yourBucketName>', connect_timeout=30)
- CRC 検証を無効化
アップロードおよびダウンロード中の CRC 検証は、アップロードおよびダウンロード中のデータ保全性を保証するためにデフォルトで有効になっています。 CRC 検証を無効にするには、次のコードを実行します。
警告 CRC 検証を無効化しないことを推奨します。 CRC 検証を無効にした場合、アップロード中およびダウンロード中のデータの整合性は保証されません。# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' bucket = oss2.Bucket(auth, endpoint, '<yourBucketName>', enable_crc=False)