All Products
Search
Document Center

Object Storage Service:Download a file (Python SDK V1)

Last Updated:Nov 28, 2025

By default, calling the GetObject operation on a versioning-enabled bucket returns only the current version of an object.

Background information

When you call the GetObject operation on a bucket, one of the following three 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To download an object, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following code provides an example of how to download a file:

# -*- 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 cyclic redundancy check (CRC) checksum of the object data is calculated only after the read() method 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 more information about the API operation for downloading a file, see GetObject.