cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

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: 

how to get file_id, file_size metadata using filename gotten from file chooser (python)

how to get file_id, file_size metadata using filename gotten from file chooser (python)

app5000
Explorer | Level 3
Go to solution

I have used dropbox file chooser to download files and filelinks.  After I extract the filenames using the file chooser, I want to get more extended metadta.

 

To do that I first went through Oauth2, then I used the following python codes:

 

fileid=dbx.files_get_metadata('/'+filename).id

 

but got the following error message:

 

 

  File "/home/serve624/lib/python2.7/site-packages/dropbox/base.py", line 449, in files_get_metadata
    None,
  File "/home/serve624/lib/python2.7/site-packages/dropbox/dropbox.py", line 272, in request
    user_message_locale)
ApiError: ApiError('f865f89a9c9f0c3e53e7319f8d99e3cf', GetMetadataError(u'path', LookupError(u'not_found', None)))

 

Here is another line of python codes:

 

filesize=dbx.files_get_metadata('/'+filename).size

 

 

  File "/home/serve624/lib/python2.7/site-packages/dropbox/base.py", line 449, in files_get_metadata
    None,
  File "/home/serve624/lib/python2.7/site-packages/dropbox/dropbox.py", line 272, in request
    user_message_locale)
ApiError: ApiError('25dcb0204364f0edcc35c33231877b85', GetMetadataError(u'path', LookupError(u'not_found', None)))

 

These two lines of python codes worked for some files from one account but not for files in another account.  I don't know what the error message means.  Could it be that the file path in the () is not correct?  If so how should I compose the file path?  How do I get the correct file path from the file chooser?

 

Thank you for your help.

 

 

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

I just took another look at your code, and I see you're trying to access the `size` returned by the Chooser, but per the documentation that should actually be `bytes`, so you can access it like `obj[0].bytes`.

 

The API v2 Python SDK's files_get_metadata method does accept a file ID in place of a path, so you can just call `dbx.files_get_metadata(fileID)`, where `fileID` is the file ID you got from the Chooser as `obj[0].id)`.

View solution in original post

10 Replies 10

Greg-DB
Dropbox Staff
Go to solution

When calling files_get_metadata like this, you need to supply the full path and name for the file. If you don't supply the path to a file that exists, you'll get this not_found error.

 

The Chooser only returns the name of the file, and not the path. So, if the file happens to exist in root, your files_get_metadata call will happen to find it. If it doesn't though, you won't have the right path, and the call will fail.

 

Alternatively, if you're using the web Chooser, you can get the "id" returned by the Chooser and pass that to files_get_metadata instead. The "id" was recently added to the Chooser though, and isn't officially documented, so I can't promise it will be reliable.

app5000
Explorer | Level 3
Go to solution
Hello Greg,
 
I am using Dropbox Javascript file chooser, can I get fileid that way?  Assume I can, how can I use fileid to get modified time, client creation time and download the thumbnail?
 
Q

Greg-DB
Dropbox Staff
Go to solution

Yes, using the JavaScript Chooser, get the 'id' from the result, just like you get the filename. Then, pass that directly into files_get_metadata.

app5000
Explorer | Level 3
Go to solution

Hello Greg,

 

Just tried but fileid came up empty.

 

Here is the code I am using to get the values from returned response:

 

filename=obj.name;
filepath=obj.link;
fileicon=obj.id;
filesize=obj.size;
fileicons=obj.icon;
filethumbnail=obj.thumbnailLink;

 

I am not sure how to save the thumbnail image.  Should I save it as a file or is it in a string (as in a link) format?

 

Q

 

 

 

 

Greg-DB
Dropbox Staff
Go to solution

Can you share the rest of your Chooser code? Accessing .id on the returned file object should give you the file ID.

 

Also, thumbnailLink is a URL that points to the data for the thumbnail. What you with that is up to you. E.g., you can make an HTTPS request to it to download the thumbnail data, and display it in your app.

app5000
Explorer | Level 3
Go to solution

Here is what I have:

 

 

<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="kuxx5m7ap1npv24"></script>
<script>
var options= {
multiselect: true,
//extensions: ['.png', '.jpg', '.mvp3', '.mvp4', '.doc', '.xls'],
linkType: 'preview',
success: function(files) {

var filenames;
var filepaths;
var fileids;
var filesizes;
var fileicons;
var filethumbnails;
 
//Parse file names and file paths from files. Store data into arrays.
var obj = files;

filenames=obj.name;
filepaths=obj.link;
fileicons=obj.id;
filesizes=obj.size;
fileicons=obj.icon;
filethumbnails=obj.thumbnailLink;
 
document.getElementById('linkfile').value = filepaths; 
document.getElementById('namefile').value = filenames;
document.getElementById('idfile').value = fileids;
document.getElementById('sizefile').value = filesizes;
document.getElementById('iconfile').value = fileicons;
document.getElementById('thumbnailfile').value = filethumbnails;
 
alert(filepaths);
alert(filenames);
alert(fileids); 
alert(filesizes);
alert(fileicons);
alert(filethumbnails);
};
},
};
$(document).ready(function() {
var button = Dropbox.createChooseButton(options);
document.getElementById('uploadDB').appendChild(button);
});
</script>
<div id="uploadDB"></div>

 

I can get name, link, icon/thumbnail not size and nor id.

 

Q.

Greg-DB
Dropbox Staff
Go to solution

Thanks! I see two things that will likely make this not work:

 

1) The `files` variable given to your `success` callback is an array of files, so you can't directly access the information about specific files on it. You'll need to either iterate over it, or access individual items, e.g., `files[0].id`.

2) You're never actually setting `fileids`. You're instead setting `fileicons` twice.

app5000
Explorer | Level 3
Go to solution

Good catch.  I tried using var obj = files[0]; to get the first file and I was able to get the fileid.  But the filesize is still empty.  Is it possible to get filesize here?  Maybe I can get it later using fileid.

 

I am trying to find instructions on getting metadata using fileid in python. program.  You are so knowledgable, do you have any hints for me?  I cannot pin point any good reads on it.   Thanks again.

 

Q

Greg-DB
Dropbox Staff
Go to solution

I just took another look at your code, and I see you're trying to access the `size` returned by the Chooser, but per the documentation that should actually be `bytes`, so you can access it like `obj[0].bytes`.

 

The API v2 Python SDK's files_get_metadata method does accept a file ID in place of a path, so you can just call `dbx.files_get_metadata(fileID)`, where `fileID` is the file ID you got from the Chooser as `obj[0].id)`.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    app5000 Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?