1

I would like to know how to use the dropbox library in Python 2.7 to upload a file to my Dropbox.

I have successfully connected to my Dropbox and the Dropbox object is called db.

How to do this?

4
  • By the dropbox object you mean the DropboxClient? Commented Apr 30, 2017 at 17:30
  • I mean the class dropbox.Dropbox() Commented Apr 30, 2017 at 17:31
  • 1
    Have you tried anything and run into a problem? If so, post your code and the error. Or are you just asking us to post a link to the documentation? Commented Apr 30, 2017 at 17:39
  • I don't believe the Dropbox class is meant for public consumption because they don't document it on their API docs - I only found it in the source code. Do you mind a solution with the DropboxClient, that is the one I'm familiar with and that's also the one that is documented for public use? Commented Apr 30, 2017 at 17:39

3 Answers 3

2

The Dropbox Python SDK provides both API v1 and API v2 functionality for backwards compatibility, but only API v2 should be used now, as API v1 is deprecated. The tutorial covers the basic of using the API v2 functionality.



This uses the Dropbox Python SDK to download a file from the Dropbox API at the remote path /Homework/math/Prime_Numbers.txt to the local file Prime_Numbers.txt:

import dropbox
dbx = dropbox.Dropbox("<ACCESS_TOKEN>")

with open("Prime_Numbers.txt", "wb") as f:
    metadata, res = dbx.files_download(path="/Homework/math/Prime_Numbers.txt")
    f.write(res.content)

<ACCESS_TOKEN> should be replaced with your access token.



And for uploading:

This uses the Dropbox Python SDK to upload a file to the Dropbox API from the local file as specified by file_path to the remote path as specified by dest_path. It also chooses whether or not to use an upload session based on the size of the file:

f = open(file_path)
file_size = os.path.getsize(file_path)

CHUNK_SIZE = 4 * 1024 * 1024

if file_size <= CHUNK_SIZE:

    print dbx.files_upload(f.read(), dest_path)

else:

    upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
                                               offset=f.tell())
    commit = dropbox.files.CommitInfo(path=dest_path)

    while f.tell() < file_size:
        if ((file_size - f.tell()) <= CHUNK_SIZE):
            print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
                                            cursor,
                                            commit)
        else:
            dbx.files_upload_session_append(f.read(CHUNK_SIZE),
                                            cursor.session_id,
                                            cursor.offset)
            cursor.offset = f.tell()

f.close()
Sign up to request clarification or add additional context in comments.

Comments

0

Note

This is API v1, which is now being deprecated. Use with care or use the currently supported APIs.

Initialize Dropbox client

import dropbox
access_token = 'SOME_ACCESS_TOKEN'
client = dropbox.client.DropboxClient(access_token)

Upload file

src_file = open('SOME_LOCAL_FILE', 'r')
response = client.put_file('SOME_REMOTE_FILE', src_file)

Download file

dest_file = open('SOME_LOCAL_FILE', 'w')
with client.get_file('SOME_REMOTE_FILE') as src_file:
    dest_file.write(src_file.read())

Reference

For more concise API documentation please refer to Core API for Python Documentation

3 Comments

Thank you very much! I will test it soon (~30 minutes) and mark this answer as accepted if it works.
This code uses the deprecated API v1, which will be turned off in 2 months.
@Rawing Thx for the heads up. I used this API years ago and I didn't realize it was being pushed out. I'll note that on my answer.
0
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 4 * 1024 * 1024

if file_size <= CHUNK_SIZE:
    print dbx.files_upload(f, dest_path)
else:
    upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))

    cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
    commit = dropbox.files.CommitInfo(path=dest_path)

    while f.tell() < file_size:
        commit = dropbox.files.CommitInfo(path=dest_path)    
        if ((file_size - f.tell()) <= CHUNK_SIZE):
            print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),                               
                                                          cursor,
                                                          commit)
        else:
            dbx.files_upload_session_append(f.read(CHUNK_SIZE),cursor.session_id, cursor.offset)
           cursor.offset = f.tell()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.