All Products
Search
Document Center

Object Storage Service:Streaming download (Python SDK V1)

Last Updated:Aug 08, 2025

If a file is too large or takes a long time to download, you can use a streaming download. This method processes the file in chunks until the download is complete.

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, 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 use streaming download, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following code shows how to perform a streaming download of the exampleobject.txt file from the examplebucket bucket.

# -*- 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 set.
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: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace yourBucketName with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# The return value of bucket.get_object is a file-like object, which is also an iterable object.
# Specify the full path of the object. The full path cannot include the bucket name.
object_stream = bucket.get_object('exampleobject.txt')
print(object_stream.read())

# The get_object operation returns a stream. You must call read() to calculate the cyclic redundancy check (CRC) of the returned object data. Therefore, you must perform a CRC check after you call the operation.
if object_stream.client_crc != object_stream.server_crc:
    print("The CRC checksum between client and server is inconsistent!")

The following code performs a streaming download of the exampleobject.txt file and saves it as examplefile.txt in the local path D:\localpath.

import shutil
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 set.
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: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace yourBucketName with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# `object_stream` is a file-like object. Use the shutil.copyfileobj method to download the streaming data to a local file.
# Specify the full path of the object. The full path cannot include the bucket name.
object_stream = bucket.get_object('exampleobject.txt')
# Download the object to a local file and save it to the specified local path. If the specified local file exists, it is overwritten. If it does not exist, a new file is created.
# If a local path is not specified, the downloaded file is saved to the local path of the project that contains the sample program.
with open('D:\\localpath\\examplefile.txt', 'wb') as local_fileobj:
    shutil.copyfileobj(object_stream, local_fileobj)
    

The following code performs a streaming copy of the exampleobject.txt object to a new object named exampleobjectnew.txt.

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 set.
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: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace yourBucketName with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# `object_stream` is an iterable object. You can copy the streaming data to another file in the same bucket.
# Specify the full path of the object. The full path cannot include the bucket name.
object_stream = bucket.get_object('exampleobject.txt')
# Specify the full path of the other object. The full path cannot include the bucket name.
bucket.put_object('exampleobjectnew.txt', object_stream)

References

  • For the complete sample code for streaming downloads, see the GitHub example.

  • For more information about the API operation for streaming downloads, see GetObject.