File: dropbox.rst

package info (click to toggle)
python-django-storages 1.14.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 896 kB
  • sloc: python: 4,548; makefile: 119; sh: 6
file content (127 lines) | stat: -rw-r--r-- 4,437 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Dropbox
=======

A Django files storage using Dropbox as a backend via the official
`Dropbox SDK for Python`_. Currently only v2 of the API is supported.

Installation
------------

Before you start configuration, you will need to install the SDK
which can be done for you automatically by doing::

   pip install django-storages[dropbox]

Configuration & Settings
------------------------

Django 4.2 changed the way file storage objects are configured. In particular, it made it easier to independently configure
storage backends and add additional ones. To configure multiple storage objects pre Django 4.2 required subclassing the backend
because the settings were global, now you pass them under the key ``OPTIONS``. For example, to save media files to Dropbox on Django
>= 4.2 you'd define::


  STORAGES = {
      "default": {
          "BACKEND": "storages.backends.dropbox.DropboxStorage",
          "OPTIONS": {
            ...your_options_here
          },
      },
  }

On Django < 4.2 you'd instead define::

    DEFAULT_FILE_STORAGE = "storages.backends.dropbox.DropboxStorage"

To put static files on Dropbox via ``collectstatic`` on Django >= 4.2 you'd include the ``staticfiles`` key (at the same level as
``default``) in the ``STORAGES`` dictionary while on Django < 4.2 you'd instead define::

    STATICFILES_STORAGE = "storages.backends.dropbox.DropboxStorage"

The settings documented in the following sections include both the key for ``OPTIONS`` (and subclassing) as
well as the global value. Given the significant improvements provided by the new API, migration is strongly encouraged.

Authentication
--------------

Two methods of authentication are supported:

#. Using an access token
#. Using a refresh token with an app key and secret

Dropbox has recently introduced short-lived access tokens only, and does not seem to allow new apps to generate access tokens that do not expire. Short-lived access tokens can be indentified by their prefix (short-lived access tokens start with ``'sl.'``).

You can manually obtain the refresh token by following the instructions below using ``APP_KEY`` and ``APP_SECRET``.

The relevant settings which can all be obtained by following the instructions in the `tutorial`_:

#. ``oauth2_access_token`` or ``DROPBOX_OAUTH2_TOKEN``
#. ``oauth2_refresh_token`` or ``DROPBOX_OAUTH2_REFRESH_TOKEN``
#. ``app_secret`` or ``DROPBOX_APP_SECRET``
#. ``app_key`` or ``DROPBOX_APP_KEY``

The refresh token can be obtained using the `commandline-oauth.py`_ example from the `Dropbox SDK for Python`_.

Get AUTHORIZATION_CODE
~~~~~~~~~~~~~~~~~~~~~~

Using your ``APP_KEY`` follow the link:

   https://www.dropbox.com/oauth2/authorize?client_id=APP_KEY&token_access_type=offline&response_type=code

It will give you ``AUTHORIZATION_CODE``.

Obtain the refresh token
~~~~~~~~~~~~~~~~~~~~~~~~

Usinh your ``APP_KEY``, ``APP_SECRET`` and ``AUTHORIZATION_KEY`` obtain the refresh token.

.. code-block:: shell

   curl -u APP_KEY:APP_SECRET \
   -d "code=AUTHORIZATION_CODE&grant_type=authorization_code" \
   -H "Content-Type: application/x-www-form-urlencoded" \
   -X POST "https://api.dropboxapi.com/oauth2/token"

The response would be:

.. code-block:: json

   {
      "access_token": "sl.************************",
      "token_type": "bearer",
      "expires_in": 14400,
      "refresh_token": "************************", <-- your REFRESH_TOKEN
      "scope": <SCOPES>,
      "uid": "************************",
      "account_id": "dbid:************************"
   }

Settings
--------

``root_path`` or ``DROPBOX_ROOT_PATH``

  Default: ``'/'``

  Path which will prefix all uploaded files. Must begin with a ``/``.

``timeout`` or ``DROPBOX_TIMEOUT``

  Default: ``100``

  Timeout in seconds for requests to the API. If ``None``, the client will wait forever.
  The default value matches the SDK at the time of this writing.

``write_mode`` or ``DROPBOX_WRITE_MODE``

  Default: ``'add'``

  Sets the Dropbox WriteMode strategy. Read more in the `official docs`_.


.. _`tutorial`: https://www.dropbox.com/developers/documentation/python#tutorial
.. _`Dropbox SDK for Python`: https://www.dropbox.com/developers/documentation/python#tutorial
.. _`official docs`: https://dropbox-sdk-python.readthedocs.io/en/latest/api/files.html#dropbox.files.WriteMode
.. _`commandline-oauth.py`: https://github.com/dropbox/dropbox-sdk-python/blob/master/example/oauth/commandline-oauth.py