このトピックでは、Object Storage Service (OSS) バケットからローカルデバイスにオブジェクトをすばやくダウンロードする方法について説明します。
使用方法
このトピックのサンプルコードでは、中国 (杭州) リージョンのリージョン ID cn-hangzhou を使用しています。デフォルトでは、パブリックエンドポイントを使用してバケット内のリソースにアクセスします。バケットが配置されているのと同じリージョン内の他の Alibaba Cloud サービスからバケット内のリソースにアクセスする場合は、内部エンドポイントを使用します。 OSS でサポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
権限
デフォルトでは、Alibaba Cloud アカウントにはフルパーミッションがあります。Alibaba Cloud アカウントの RAM ユーザーまたは RAM ロールには、デフォルトではパーミッションがありません。Alibaba Cloud アカウントまたはアカウント管理者は、「RAM ポリシー」または「バケットポリシー」を通じて操作権限を付与する必要があります。
API | アクション | 定義 |
GetObject |
| オブジェクトをダウンロードします。 |
| オブジェクトをダウンロードするときに、versionId を使用してオブジェクトバージョンを指定する場合、この権限が必要です。 | |
| オブジェクトをダウンロードするときに、オブジェクトメタデータに X-Oss-Server-Side-Encryption: KMS が含まれている場合、この権限が必要です。 |
メソッド定義
get_object(request: GetObjectRequest, **kwargs) → GetObjectResultリクエストパラメーター
パラメーター | タイプ | 説明 |
request | GetObjectRequest | リクエストパラメーター。詳細については、GetObjectRequest をご参照ください。 |
レスポンスパラメーター
タイプ | 説明 |
GetObjectResult | 戻り値。詳細については、GetObjectResult をご参照ください。 |
簡単なダウンロードメソッドの完全な定義については、「get_object」をご参照ください。
サンプルコード
次のサンプルコードは、オブジェクトをローカルデバイスにダウンロードします。
import argparse
import alibabacloud_oss_v2 as oss
import os
# Create a command-line parameter parser.
parser = argparse.ArgumentParser(description="get object sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter to indicate the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter to indicate the name of the object. This parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
def main():
# Parse the command-line parameters.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configuration of the SDK and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the bucket is located.
cfg.region = args.region
# If an endpoint is provided, specify the endpoint in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configuration to create an OSSClient instance.
client = oss.Client(cfg)
# Execute the request to download the object, and specify the bucket name and object name.
result = client.get_object(oss.GetObjectRequest(
bucket=args.bucket, # Specify the name of the bucket.
key=args.key, # Specify object key.
))
# Display the response to check whether the request is successful.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content length: {result.content_length},'
f' content range: {result.content_range},'
f' content type: {result.content_type},'
f' etag: {result.etag},'
f' last modified: {result.last_modified},'
f' content md5: {result.content_md5},'
f' cache control: {result.cache_control},'
f' content disposition: {result.content_disposition},'
f' content encoding: {result.content_encoding},'
f' expires: {result.expires},'
f' hash crc64: {result.hash_crc64},'
f' storage class: {result.storage_class},'
f' object type: {result.object_type},'
f' version id: {result.version_id},'
f' tagging count: {result.tagging_count},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' next append position: {result.next_append_position},'
f' expiration: {result.expiration},'
f' restore: {result.restore},'
f' process status: {result.process_status},'
f' delete marker: {result.delete_marker},'
)
# ========== Method 1: Read the entire object ==========
with result.body as body_stream:
data = body_stream.read()
print(f"The object is read. Data length: {len(data)} bytes")
path = "./get-object-sample.txt"
with open(path, 'wb') as f:
f.write(data)
print(f"The object is downloaded and saved to a local path: {path}")
# # ========== Method 2: Read the object in chunks ==========
# with result.body as body_stream:
# chunk_path = "./get-object-sample-chunks.txt"
# total_size = 0
# with open(chunk_path, 'wb') as f:
# # Use 256KB block size (you can adjust the block_size parameter as needed)
# for chunk in body_stream.iter_bytes(block_size=256 * 1024):
# f.write(chunk)
# total_size += len(chunk)
# print(f"Received data block: {len(chunk)} bytes | Total: {total_size} bytes")
# print(f"The object is downloaded and saved to the local path: {chunk_path}")
# Call the main function when the script is directly run.
if __name__ == "__main__":
main() # The entry point of the script. When the script is directly run, the main function is called.一般的なシナリオ
オブジェクトをローカルファイルにバッチダウンロードする
関連情報
簡単なダウンロードの完全なサンプルコードについては、「get_object.py」および「get_object_to_file.py」をご参照ください。