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 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.
Examples
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
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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 and iterable object.
# 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
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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 object 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
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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)