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?
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()
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
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()
the dropbox objectyou mean theDropboxClient?Dropboxclass 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 theDropboxClient, that is the one I'm familiar with and that's also the one that is documented for public use?