Download Object

Download an Object

The SDKs present in the section will allow you to download a particular object, multiple objects, or version of the object. The Bucket reference used in the below code snippet is the component instance.

The first step of the download operation is a GET operation that retrieves the required object from the bucket.

To be able to download an object, the requester must have READ access permissions. However, owners of the bucket do have the option to grant READ access permissions to users, allowing them to download the object without using the required response headers.

If Versioning is enabled for your bucket, you need to pass the versionId to download the particular version of the object. If no versionId is passed, then by default, the latest version of the object will be downloaded.

If Versioning was enabled for a bucket, then disabled. By default, the principal first object will be downloaded. To ensure you download the latest version of this object, you need to pass the versionId param with the value “topVersion”.

    
copy
res = bucket.get_object("sam/out/sample.txt") # download the object to local machine file = open('file_path','wb') file.write(res)

Download a Portion of the Object

The following SDK method is used with the range parameter. The range parameter allows you to download a specific range of bytes of an object.

    
copy
options = { 'version_id': '01hx66f1383jm48w9sa4z20kve', # download the object with given version Id 'range': '0-200' # start and end range of an object } res = bucket.get_object('"sam/out/sample.txt", options) # download the object to local machine file = open('file_path','wb') file.write(res)

Download an Object Using Transfer Manager

In this section, we are going to go over SDK methods that will allow you to successfully download large objects from Stratus to your local system using Transfer Manager technique. Transfer Manager is an operation where the large object is split into multiple byte ranges using the start and end bytes range of the object. Each of the object’s parts is then returned as a stream, and they are downloaded to your local system.

Get Transfer Manager Instance

To perform transfer manager operations, you need to get a transfer manager object instance. We will refer to this component instance in various code snippets where we work with transfer manager operations being performed on objects stored in a bucket in Stratus.

Parameter Used

bucket: This is the bucket instance you need to have initialized earlier using this SDK method.

Ensure the following packages are imported

    
copy
from zcatalyst_sdk.stratus.transfer_manager import TransferManager
    
copy
transfer_manager = TranferManager(bucket)

Get Iterable Object

It return the iterable object parts. User can write these parts to our local machine using iterator.

Note: Use single file to write because it return iterable parts not whole object. If you write in individual files, you need to combine them in to one.
Info: Ensure you provide part_size in Mb.
    
copy
res = transfer_manager.get_iterable_object("sam/out/sample.txt", 20) file = open('file_path','wb') # store the object to local machine for chunk in res: file.write(chunk)

Generate Object Part Downloaders

When a user needs to download a large file (example: 10 GB file), using the get_object() method is not practical. Instead, the user can opt for this SDK method. By calling the generate_part_downloaders() method with the parameters key(str) and part_size(Long), the user can obtain transfer manager functions. These functions, returned in ascending order, each download a specific part of the object. The parts are determined based on the specified part size.

    
copy
res = transfer_manager.generate_part_downloaders("sam/out/sample.txt",20) file = open('file_path','wb') for part in res: file.write(part())
Info: The file must be uploaded via the API or SDK to enable partial downloads. The part_size should be greater than 5 MB and less than 100 MB.

Generate Presigned URL to Download an Object

Presigned URLs are secure URLs that authenticated users can share to their non-authenticated users. This URL will provide non-authenticated users with temporary authorization to access objects. The Bucket reference used in the below code snippet is the component instance.

Info: To use this SDK method, you need intialize it with Admin scope. You can learn more about this requirement from this section

Parameters Used

Parameter Name Data Type Definition
key String A Mandatory parameter. Will hold the complete name of the object along with it's path.
url_action Request Method A Mandatory parameter. This is the parameter that will allow you to generate a presigned URL for a download(GET) action.
  • GET: To download an object
expiry String This is an Optional parameter. The URL validity time in seconds.
  • Default value: 3600 seconds
  • Minimum value: 30 seconds
  • Maximum value: 7 days
active_from String This is an Optional parameter. This param will contain the time after which the URL is valid. Maximum value is 7 days. URLs are made active as soon as they are generated by default.
    
copy
pre_signed_url_res = bucket.generate_presigned_url("sam/out/sample.txt",url_action='GET',expiry_in_sec='300', active_from='1023453725828', version_id='jdery748tfge78') print(pre_signed_url_res)

Example Response for Generating a Presigned URL for Download

    
copy
{ "signature": "https://zcstratus123-development.zohostratus.com/_signed/sam/out/sample.txt?organizationId=83963316&stsCredential=74660608-83963316&stsDate=1726492859577&stsExpiresAfter=300&stsSignedHeaders=host&stsSignature=_G8mnq-03vKgPlnJPmqBvzEnT3Hk-SnECuG-cgURyDs", "expiry_in_seconds": "300", "active_from": "1726492859577" }

Example Snippet Illustrating Usage of Presigned URL to Download an Object

    
copy
import requests #Pre-signed URL to download the file url = "https://sadi-development.zohostratus.com/_signed/code.txt?organizationId=96862383&stsCredential=96858154-96862383&stsDate=1747899927592&stsExpiresAfter=300&stsSignedHeaders=host&stsSignature=-l10AlSsbZzkq6t8HHgDfNkEiiDWFaaU9M3-hPBz0M8" #Path where the downloaded file will be saved locally file_path = "file_path" # Replace with actual file path #Send a GET request to download the file response = requests.get(url, stream=True) #Check if the request was successful if response.status_code == 200: #Open the destination file in binary write mode with open(file_path, "wb") as f: #Write data in chunks to handle large files for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) print("Download completed successfully.") else: print("Object download failed. Status code:", response.status_code)

Last Updated 2025-06-03 12:50:19 +0530 +0530