Python SDK Samples - Files Operations
Upload Files
Get File
import os
from zcrmsdk.src.com.zoho.crm.api.file import *
from zcrmsdk.src.com.zoho.crm.api import ParameterMap
from zcrmsdk.src.com.zoho.crm.api.util import StreamWrapper
class File(object):
@staticmethod
def get_file(id, destination_folder):
"""
This method is used to download the file with ID and write in the destinationFolder
:param id: The ID of the uploaded File.
:param destination_folder: The absolute path of the destination folder to store the File
"""
"""
example
id = "ae9c7cefa418aec1d6a5cc2d9ab35c3231aae3bfeef7d5e00a54b7563c0dd42b";
destination_folder = "/Users/user_name/Desktop"
"""
# Get instance of FileOperations Class
file_operations = FileOperations()
# Get instance of ParameterMap Class
param_instance = ParameterMap()
# Add the id to ParameterMap instance
param_instance.add(GetFileParam.id, id)
# Call get_file method that takes ParameterMap instance as parameter
response = file_operations.get_file(param_instance)
if response is not None:
# Get the status code from response
print('Status Code: ' + str(response.get_status_code()))
if response.get_status_code() in [204, 304]:
print('No Content' if response.get_status_code() == 204 else 'Not Modified')
return
# Get object from response
response_object = response.get_object()
if response_object is not None:
# Check if expected FileBodyWrapper instance is received.
if isinstance(response_object, FileBodyWrapper):
# Get StreamWrapper instance from the returned FileBodyWrapper instance
stream_wrapper = response_object.get_file()
# Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
file_name = os.path.join(destination_folder, stream_wrapper.get_name())
# Open the destination file where the file needs to be written in 'wb' mode
with open(file_name, 'wb') as f:
# Get the stream from StreamWrapper instance
for chunk in stream_wrapper.get_stream():
f.write(chunk)
f.close()
# Check if the request returned an exception
elif isinstance(response_object, APIException):
# Get the Status
print("Status: " + response_object.get_status().get_value())
# Get the Code
print("Code: " + response_object.get_code().get_value())
print("Details")
# Get the details dict
details = response_object.get_details()
for key, value in details.items():
print(key + ' : ' + str(value))
# Get the Message
print("Message: " + response_object.get_message().get_value())