All Products
Search
Document Center

Object Storage Service:Streaming download

Last Updated:Oct 16, 2023

Streaming download allows you to download an object as streams in increments. If you want to download a large object or if the download requires a long period of time to complete, you can perform streaming download to download the object in increments.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Common examples of RAM policies.

Sample code

The following sample code provides an example on how to perform streaming download to download an object named exampleobject.txt from a bucket named examplebucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Create a server object. 
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# The value returned by bucket.get_object is a file-like object that can be iterated. 
# Specify the full path of the object. Do not include the bucket name in the full path of the object. 
object_stream = bucket.get_object('exampleobject.txt')
print(object_stream.read())

# You must call the read() operation to read the object from the stream returned by get_object before you can calculate the CRC-64 value of the object. 
if object_stream.client_crc != object_stream.server_crc:
    print("The CRC checksum between client and server is inconsistent!")

The following sample code provides an example on how to download the data of an object named exampleobject.txt from a stream as a file named examplefile.txt in the D:\localpath directory:

import shutil
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Create a server object. 
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# object_stream is a file-like object. You can use the shutil.copyfileobj method to download the data from a stream to the local file. 
# Specify the full path of the object. Do not include the bucket name in the full path of the object. 
object_stream = bucket.get_object('exampleobject.txt')
# Download the object as a local file in the specified path. If a file that has the same name already exists in the specified path, the downloaded object overwrites the file. Otherwise, the downloaded file is saved in the path. 
# If you do not specify a path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs. 
with open('D:\\localpath\\examplefile.txt', 'wb') as local_fileobj:
    shutil.copyfileobj(object_stream, local_fileobj)

The following sample code provides an example on how to copy the data of an object named exampleobject.txt from a stream to an object named exampleobjectnew.txt:

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Create a server object. 
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# object_stream is an iterable object. You can copy the data of the object from a stream to another object in the same bucket. 
# Specify the full path of the object. Do not include the bucket name in the full path of the object. 
object_stream = bucket.get_object('exampleobject.txt')
# Specify the full path of the destination object. Do not include the bucket name in the full path of the object. 
bucket.put_object('exampleobjectnew.txt', object_stream)

References

  • For the complete sample code that is used to perform streaming download, visit GitHub.

  • For more information about the API operation that you can call to perform streaming download, see GetObject.