Downloading a file
Start the Python shell and configure its session.
The following variables should be available now:
>>> base_url # the base URL of the API 'https://eu2-cloud.acronis.com/api/notary/v2' >>> auth # the 'Authorization' header value with the access token {'Authorization': 'Bearer 8770b34b74f9e4d9424eff50c38182bb4ae7f5596582ae61900b1b6a23e3ec58'}
Define a variable named
file_id
, and then assign the ID of the required file to this variable:>>> file_id = 'cc3ecc8d-de0e-4809-8bc2-3a6368110824'
Download the file by sending a GET request to the
/stored-files/{file_id}/download
endpoint:>>> response = requests.get(f'{base_url}/stored-files/{file_id}/download', headers=auth)
Check the status code of the response:
>>> response.status_code 200
Status code 200 means that the notary service has started streaming the file contents to you. If the status code is 404, there is no file with such an ID in the storage. You may have incorrectly specified the ID or the file has been deleted from the storage.
A different status code means that an error has occurred. For the details, refer to “Status and error codes”.
Fetch the file name from the value of the
Content-Disposition
header of the response:Define a variable named
content_disposition
, and then assign the value of theContent-Disposition
header fetched from the response to this variable:>>> content_disposition = response.headers['Content-Disposition'] >>> content_disposition 'attachment;filename=lorem_ipsum.txt'
Parse the file name from the header value stored in the
content_disposition
variable:>>> import re # Provides regular expression matching operations. >>> file_name = re.search('filename=(.+)', content_disposition).group(1) >>> file_name 'lorem_ipsum.txt'
Save the data being streamed by the service to a file:
>>> with open(file_name, 'wb') as fd: ... for chunk in response.iter_content(chunk_size=1024): ... if chunk: # filter out keep-alive new chunks ... fd.write(chunk) 446
The returned value is the number of bytes written to the file.
[Optional] Ensure that the value of written bytes is equal to the size of the file in the storage by fetching the information about the file, and then checking the
size
key in the JSON object of the response.