Downloading a file

  1. 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'}
    
  2. 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'
    
  3. 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)
    
  4. 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”.

  5. Fetch the file name from the value of the Content-Disposition header of the response:

    1. Define a variable named content_disposition, and then assign the value of the Content-Disposition header fetched from the response to this variable:

      >>> content_disposition = response.headers['Content-Disposition']
      >>> content_disposition
      'attachment;filename=lorem_ipsum.txt'
      
    2. 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'
      
  6. 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.

  7. [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.

Full code example

 1#!/usr/bin/env python3
 2
 3import requests  # Will be used for sending requests to the API.
 4import hashlib   # Will be used for calculating hash values.
 5import os.path   # Will be used for path-related operations.
 6import pprint    # Will be used for formatting the output of JSON objects received in API responses.
 7import json      # Will be used for converting dictionaries into JSON text
 8
 9# Define variables named "LOGIN" and "PASSWORD" and then assign them with your account credentials
10LOGIN = '<your login>'        # Change login here
11PASSWORD = '<your password>'  # Change password here
12
13# Define a variable named "cloud_url" and then assign it with the URL of the cloud platform
14cloud_url = 'https://cloud.acronis.com'
15
16# Fetch the URL of the data center where your account is located by sending a GET request to the "/api/1/accounts" endpoint
17response = requests.get(
18    f'{cloud_url}/api/1/accounts',
19    params={'login': LOGIN}
20)
21response.raise_for_status()
22
23# Convert the JSON text that the response body contains to a dictionary and store the data center URL
24# in a variable that will be used in further requests
25server_url = response.json()['server_url']
26
27# Define a variable named "account_creds", and then assign the username and password to this variable
28account_creds = {
29    'username': LOGIN,
30    'password': PASSWORD
31}
32
33# Generate a token by sending a POST request to the "/api/2/idp/token" with your account credentials to the cloud platform
34response = requests.post(
35    f'{server_url}/api/2/idp/token',
36    headers={'Content-Type': 'application/x-www-form-urlencoded'},
37    data={'grant_type': 'password', **account_creds}
38)
39response.raise_for_status()
40
41# Convert the JSON text that the response body contains to a dictionary and then assign it to a variable named "token_info"
42token_info = response.json()
43
44# Define a variable named "auth" and then assign it with a dictionary with "Authorization" key containing
45# token string formatted as "Bearer <access_token>"
46auth = {
47    'Authorization': 'Bearer ' + token_info['access_token']
48}
49
50# Define a variable named "base_url", and then assign the API base URL using the data center URL
51# to this variable
52base_url = f'{server_url}/api/notary/v2'
53
54# Define a variable named "file_id" and then assign it with the ID of the file
55file_id = 'cc3ecc8d-de0e-4809-8bc2-3a6368110824'
56
57# Download the file by sending a GET request to the "/stored-files/{file_id}/download" endpoint
58response = requests.get(f'{base_url}/stored-files/{file_id}/download', headers=auth)
59response.raise_for_status()
60
61# Define a variable named "content_disposition" and store the value of the "Content-Disposition"
62# header fetched from the response
63content_disposition = response.headers['Content-Disposition']
64
65# Import "re" module that provides regular expression matching operations
66import re
67
68# Define a variable named "file_name", parse the file name from the header value stored in
69# the "content_disposition" variable and fetch matching group that is the name of the file
70file_name = re.search('filename=(.+)', content_disposition).group(1)
71
72# Save the data being streamed by the service to a file
73with open(file_name, 'wb') as fd:
74    for chunk in response.iter_content(chunk_size=1024):
75        if chunk:  # filter out keep-alive new chunks
76            fd.write(chunk)