Download a specific version of an object from a versioning-enabled bucket by passing a version ID to get_object.
Notes
The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you access the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account have no permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.
The GetObject operation requires the following permissions:
| Permission | When required |
|---|---|
oss:GetObject | Always required |
oss:GetObjectVersion | Required when specifying a version ID |
kms:Decrypt | Required when the object metadata contains X-Oss-Server-Side-Encryption: KMS |
Versioning behavior
When you call GetObject on a versioning-enabled bucket, OSS returns one of the following results:
No version ID specified: OSS returns the current version. If the current version is a delete marker, OSS returns
404 Not Found.Version ID specified: OSS returns that specific version. If the version ID is
null, OSS returns the version with a null version ID.Version ID of a delete marker specified: OSS returns
405 Method Not Allowed.
Sample code
The following sample code downloads a specific version of an object and saves it to a local file.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="get object 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 endpoint of the region in which the bucket is located.')
parser.add_argument('--key', help='The name of the object.', required=True)
parser.add_argument('--version_id', help='The version ID of the object.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default SDK configuration and set the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
# Download the specified version of the object.
result = client.get_object(oss.GetObjectRequest(
bucket=args.bucket,
key=args.key,
version_id=args.version_id,
))
# Print response metadata.
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},'
)
# Read the object content and save to a local file.
content = result.body.read()
with open('./test.txt', 'wb') as f:
f.write(content)
print('File saved to test.txt successfully!')
if __name__ == "__main__":
main()Replace the following placeholders before running:
| Parameter | Description | Example |
|---|---|---|
--region | The region where the bucket is located | cn-hangzhou |
--bucket | The bucket name | examplebucket |
--key | The object name (key) | exampledir/exampleobject.txt |
--version_id | The version ID of the object to download | CAEQHhiBgMDaabc... |
--endpoint | (Optional) Custom endpoint. Use an internal endpoint when accessing OSS from another Alibaba Cloud service in the same region to reduce traffic costs. For endpoint formats, see Regions and endpoints. | oss-cn-hangzhou-internal.aliyuncs.com |
Credentials are loaded from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables. Do not hardcode credentials in your code.
References
For the complete sample code, see get_object.py.