すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:OSS SDK for Pythonの使用を開始する

最終更新日:Nov 04, 2024

このトピックでは、Object Storage Service (OSS) SDK for Pythonを使用して、バケットの作成、オブジェクトのアップロード、オブジェクトのダウンロードなどのルーチン操作を実行する方法について説明します。

ビデオチュートリアル

次のビデオは、OSS SDK for Pythonの使用方法を示しています。

バケットを作成する

バケットは OSS のグローバルネームスペースです。 バケットは、オブジェクトを格納するために使用されるコンテナです。

説明

エンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。 バケットの命名規則の詳細については、用語トピックのバケットセクションを参照してください。

次のサンプルコードは、バケットの作成方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Set the access control list (ACL) of the bucket to private. 
bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE)

バケットの作成方法の詳細については、「バケットの作成」をご参照ください。

オブジェクトのアップロード

次のサンプルコードは、ストリーミングアップロードを使用してオブジェクトをOSSにアップロードする方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Upload the object to OSS. 
# Replace yourObjectName with the full path to the object. Include the extension in the full path. Do not include the bucket name in the full path. Example: abc/efg/123.jpg. 
# Replace yourLocalFileName with the full path to the local file. Include the file name and extension in the full path. Example: /users/local/myfile.txt. 
bucket.put_object_from_file('yourObjectName', 'yourLocalFile')

オブジェクトのダウンロード

次のサンプルコードは、特定のオブジェクトをコンピュータにダウンロードする方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Download the object to your computer. 
# Replace yourObjectName with the full path to the object. Include the extension in the full path. Do not include the bucket name in the full path. Example: abc/efg/123.jpg. 
# Replace yourLocalFileName with the full path to the local file. Include the file name and extension in the full path. Example: /users/local/myfile.txt. 
bucket.get_object_to_file('yourObjectName', 'yourLocalFile')

オブジェクトの一覧表示

次のサンプルコードは、特定のバケットに10個のオブジェクトを一覧表示する方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from itertools import islice

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# oss2.ObjectIteratorr is used to traverse objects. 
for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)

オブジェクトの削除

次のサンプルコードは、オブジェクトを削除する方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Replace yourObjectName with the full path to the object. Include the extension in the full path. Do not include the bucket name in the full path. Example: abc/efg/123.jpg. 
bucket.delete_object('yourObjectName')

オブジェクトを削除する方法の詳細については、「オブジェクトの削除」をご参照ください。