Deleting multiple files

  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 files_data, and then assign an object containing the list of the file IDs in the file_ids key to this variable:

    >>> files_data = {
    ...     'file_ids': [
    ...         'aa95bac7-33fd-4faa-9fa1-f4baeaca82cd',
    ...         'ba635289-d3a1-45a2-a0ff-a8518607f656'
    ...     ]
    ... }
    
  3. Convert the files_data object to a JSON text:

    >>> files_data = json.dumps(files_data, indent=4)
    >>> print(files_data)
    {
        "file_ids": [
            "aa95bac7-33fd-4faa-9fa1-f4baeaca82cd",
            "ba635289-d3a1-45a2-a0ff-a8518607f656"
        ]
    }
    
  4. Send a DELETE request with the JSON text to the /stored-files endpoint:

    >>> response = requests.delete(
    ...     f'{base_url}/stored-files',
    ...     headers={'Content-Type': 'application/json', **auth},
    ...     data=files_data,
    ... )
    
  5. Check the status code of the response:

    >>> response.status_code
    200
    

    Status code 200 means that the notary service has deleted the files from the storage and decreased the usage of the Notary storage quota. If the files were signed or were being signed, their documents and the PDF files of the signature certificate have been also deleted.

    A different status code means that an error has occurred. For the details, refer to “Status and error codes”.

    Also, the response body contains the deleted key containing an array of details about the deleted files formatted as a JSON text. When converted to an object, it will look as follows:

    >>> pprint.pprint(response.json())
    {'deleted': [{'cert': '66692ad42b5e1c8baed89b225321182537164c46be6cb232f09b0121af8313b7',
                  ...
                  'id': 'aa95bac7-33fd-4faa-9fa1-f4baeaca82cd',
                  ...},
                 {'cert': '6cb238637164c46bebaed89b225321182572f09b0121af8313b6692ad42b5e1c',
                  ...
                  'id': 'ba635289-d3a1-45a2-a0ff-a8518607f656',
                  ...}]}
    
  6. Fetch the information about the files, and then save the notarization certificate IDs stored in the cert key. You will always be able to fetch this certificate by sending the GET request to the /certificates/{certificate_id} endpoint or to access the certificate web page at: https://eu2-cloud.acronis.com/notary/certificate/{certificate_id}

    >>> deleted_files = response.json()['deleted']
    >>> for file in deleted_files:
    ...     print(file['cert'])
    

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 "files_data"
55# and then assign it with a dictionary with "file_ids" key containing an array of file IDs
56files_data = {
57    'file_ids': [
58        'aa95bac7-33fd-4faa-9fa1-f4baeaca82cd',
59        'ba635289-d3a1-45a2-a0ff-a8518607f656'
60    ]
61}
62
63# Convert the "files_data" dictionary to a JSON text
64files_data = json.dumps(files_data, indent=4)
65
66# Delete the files by sending a DELETE request to the "/stored-files" endpoint
67response = requests.delete(
68    f'{base_url}/stored-files',
69    headers={'Content-Type': 'application/json', **auth},
70    data=files_data,
71)
72response.raise_for_status()