Download a file (Python SDK V1)
In a versioning-enabled bucket, the GetObject operation returns only the current version of an object by default. You can also download a specific version by specifying its version ID.
Background information
When you call the GetObject operation on a bucket, one of the following cases applies:
-
If the current version of the object is a delete marker, OSS returns 404 Not Found.
-
If you specify an object's versionId in the query parameter, the specified version is returned. If you set the versionId to "null", the object version that has a null versionId is returned.
-
If you try to retrieve a delete marker by specifying its versionId, OSS returns 405 Method Not Allowed.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.
-
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.
To download an object, you must have the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Sample code
The following sample code shows how to download a specific version of an object:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this 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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace yourBucketName with the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Download a specific version of an object.
params = dict()
params['versionId'] = '<yourObjectVersionId>'
object_stream = bucket.get_object('<yourObjectName>', params=params)
# Read the content of the downloaded object.
read_content = object_stream.read()
print('get object content:', read_content)
# View the version ID of the downloaded object.
print('get object versionid:', object_stream.versionid)
# The get_object operation returns a stream. The CRC checksum is calculated only after read() is called.
# Therefore, you must perform a CRC check after the call.
if object_stream.client_crc != object_stream.server_crc:
print("The CRC checksum between client and server is inconsistent!")
References
For the API details, see GetObject.