Created
March 6, 2018 23:21
-
-
Save PetrGlad/7da5479318b57ce0aabb16c9787e8fff to your computer and use it in GitHub Desktop.
Upload large files to dropbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dropbox | |
import dropbox.files | |
CHUNK_SIZE = 4 * 1024 * 1024 | |
def upload_stream(dbx, readable, dest_path): | |
d = readable.read(CHUNK_SIZE) | |
offset = len(d) | |
session = dbx.files_upload_session_start(d) | |
cursor = dropbox.files.UploadSessionCursor(session_id=session.session_id, | |
offset=offset) | |
while True: | |
d = readable.read(CHUNK_SIZE) | |
offset += len(d) | |
if len(d) == 0: | |
commit = dropbox.files.CommitInfo(path=dest_path) | |
return dbx.files_upload_session_finish(d, cursor, commit) | |
dbx.files_upload_session_append_v2(d, cursor) | |
cursor.offset = offset | |
def upload_file(dbx, file_path, dest_path): | |
with open(file_path, 'rb') as f: | |
return upload_stream(dbx, f, dest_path) | |
if __name__ == '__main__': | |
APP_TOKEN = 'asdfasdfkjhaslkdjfhlaksjhrwqkejnbr,mwqnebr' | |
dbx = dropbox.Dropbox(APP_TOKEN) | |
print(upload_file(dbx, 'file_queries.sql', '/file_queries_there.sql')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment