cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Tell us what you want to see on the Community here!
Close

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python SDK File Uploading - Working with the Response

Highlighted

Python SDK File Uploading - Working with the Response

New member | Level 2

Hi there,

I am new to interfacing with the Dropbox API and am using Python 3.6

I am uploading large files to my account using the following function called dropboxupload (not my own) .  I simply call it at the end of my code:

import os
import sys
from tqdm import tqdm
import dropbox

filenameonly = sys.argv[1]
filenamewithextension = sys.argv[2]
fullfilepath = sys.argv[3]

access_token = "removedforsecurity"

def dropboxupload(access_token,file_path,target_path,timeout=3600,chunk_size=150 * 1024 * 1024,):
    dbx = dropbox.Dropbox(access_token, timeout=timeout)
    with open(file_path, "rb") as f:
        file_size = os.path.getsize(file_path)
        chunk_size = 150 * 1024 * 1024
        if file_size <= chunk_size:
            print(dbx.files_upload(f.read(), target_path))
        else:
            with tqdm(total=file_size, desc="Uploaded") as pbar:
                upload_session_start_result = dbx.files_upload_session_start(
                    f.read(chunk_size)
                )
                pbar.update(chunk_size)
                cursor = dropbox.files.UploadSessionCursor(
                    session_id=upload_session_start_result.session_id,
                    offset=f.tell(),
                )
                commit = dropbox.files.CommitInfo(path=target_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()
                    pbar.update(chunk_size)
					
dropboxupload(access_token, fullfilepath, "/"+filenamewithextension)

It works perfectly.

The response that comes back automatically is the FileMetadata; example as follows:

FileMetadata(name='500.mp4', id='id:a5yk0DAylOAAAAAAAAADbA', client_modified=datetime.datetime(2020, 2, 19, 13, 56, 31), server_modified=datetime.datetime(2020, 2, 19, 13, 56, 31), rev='59eee27d89b134c0970a9', size=512727070, path_lower='/500.mp4', path_display='/500.mp4', parent_shared_folder_id=None, media_info=None, symlink_info=None, sharing_info=None, is_downloadable=True, export_info=None, property_groups=None, has_explicit_shared_members=None, content_hash='e134be9c61057696145fbe4c416d5e62c983649bb756c9e9597d982781a1cb83')

What i want to do is capture this response and use the data within it for other means in the next step of my project.

For example i want to take the id and set as a new variable called "data". I have tried doing this with

data = str(dropbox.files.FileMetadata.id)
print ("THE DATA VARIABLE IS")
print(data)

but all that gets printed is....

THE DATA VARIABLE IS
property object at 0x03A82F60

I am obviously capturing the data wrong - please can you help?!

Thanks

 

8 Replies 8
Highlighted

Re: Python SDK File Uploading - Working with the Response

Super Collaborator | Level 20

Hi @yekootmada,

Your code will work when you refer to actual metadata! Where in the code you get the uploaded file's metadata?! Only in the function you use! This function don't return anything yet. Just return the metadata as a result (small modification) and use it further after.

Out of your question: That function has a hidden BUG!!! Be careful! Even not very likely, it's possible the size of a big file to be exactly equal to multiple of the chunk size. In such a case transmission will never be finalized! No check in the algorithm for such a situation and finalization left not guaranteed.

Hope this gives a direction.

Highlighted

Re: Python SDK File Uploading - Working with the Response

New member | Level 2

Thank you! 

That is the part I am struggling with! I cannot find the correct syntax to return the response as data.

I tried

data = dropbox.files.FileMetadata.id

print("THE DATA VARIABLE IS")
print(data)

but the result is

THE DATA VARIABLE IS
property object at 0x03A82F60

 

Highlighted

Re: Python SDK File Uploading - Working with the Response

Dropboxer

[Cross-linking for reference: https://stackoverflow.com/questions/60302382/python-dropbox-upload-working-with-the-response ]

The 'dropbox.files.FileMetadata' is just the class, and not an instance containing the information for this uploaded file. That is, you'll want to do something like this (or return it and use it later like Здравко said):

file_metadata = dbx.files_upload_session_finish(f.read(CHUNK_SIZE), cursor, commit)
print("This is the id of the uploaded file:")
print(file_metadata.id)
Highlighted

Re: Python SDK File Uploading - Working with the Response

Super Collaborator | Level 20

If you are newbie, learn some basics of Python syntax, at least. On your question: there is keyword 'return', as in many other languages (like C/C++, Java, Kotlin and others), which denote the returning value. This keyword could be used more than once, whenever the algorithm have a value ready to return. When such function gets called it carries a returned value which could be used directly or assigned to a variable. Follows some pseudo code:

def funcName(param1, param2):
    ...do some calculations...
    if some.check():
        return param1+param2
    ...do something else...
    result = param1 * param2
    ...possible finalization...
    return result

resValue = funcName(3, 5)
...do something using resValue here...

Hope this casts some extra light.

Highlighted

Re: Python SDK File Uploading - Working with the Response

New member | Level 2

great thanks Greg.

Still cant find where that should go in, everytime I try its giving me inconsistent use of tabs, spaces and indentations error.

GRRR

Highlighted

Re: Python SDK File Uploading - Working with the Response

Super Collaborator | Level 20
Highlighted

Re: Python SDK File Uploading - Working with the Response

Dropboxer

The files_upload_session_finish method returns the FileMetadata instance you want, so you should update your use of files_upload_session_finish. In your original code, you're just printing it out, but you can instead save it to a variable and use it there, or return it from your dropboxupload method and use it later.

Highlighted

Re: Python SDK File Uploading - Working with the Response

New member | Level 2

Cracked it.

 

Thanks all!

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?