If you need 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.

Sample code

The following 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 endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# The value returned by bucket.get_object is a file-like and iterable object. 
# Specify the full path of the object. The full path cannot contain the bucket name. 
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 code provides an example on how to download the data of an object named exampleobject.txt from a stream to 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 endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', '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. The full path cannot contain the bucket name. 
object_stream = bucket.get_object('exampleobject.txt')
# Download the object to a local file in the specified path. If a file that has the same name already exists, the downloaded object overwrites the file. If no file that has the same name exists, a new file is created. 
# 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 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 endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', '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. The full path cannot contain the bucket name. 
object_stream = bucket.get_object('exampleobject.txt')
# Specify the full path of the destination object. The full path cannot contain the bucket name. 
bucket.put_object('exampleobjectnew.txt', object_stream)

References

  • For more information about the complete sample code for streaming download, visit GitHub.
  • For more information about the API operation that you can call to perform streaming download, see GetObject.