Google Drive API - Get the id of a file when Insert - Python

90 views
Skip to first unread message

Rameez Raja

unread,
Oct 30, 2016, 12:06:01 PM10/30/16
to Google App Engine
I wrote a piece of code in python to upload a file to google drive, which was successful. But I cant able to fetch the file id after the upload. Could you guys please help me to get that.


.
.
.
DRIVE
= build('drive', 'v2', http=http)

FILES
= (
   
('sample.csv', True),
)

for filename,convert in FILES:
    metadata
= {'title': filename}
    res
= DRIVE.files().insert(convert=convert, body=metadata, media_body=filename,fields='mimeType').execute()
   
print(res)
   
 print(a)
     if res:
         print('Uploaded "%s" (%s)' % (filename, res['mimeType']))



Thank you

Alex (Cloud Platform Support)

unread,
Nov 1, 2016, 10:30:44 AM11/1/16
to Google App Engine

Hi there,


Assuming you have provided the right Authorization Scope to access the Google Drive API, here is a method(adapted from this link) to retrieve the id of the last file you uploaded:


results = DRIVE.files().list(maxResults=1).execute()

   items = results.get('items', [])

   if not items:

       print('No files found.')

   else:

       print('Files:')

       for item in items:

           print('{0} ({1})'.format(item['title'], item['id']))


Note that this type of question is more appropriate for Stack Overflow which we regularly monitor as other types of stack exchange services and which would increase your chances to get a faster answer due to its larger audience.
Reply all
Reply to author
Forward
0 new messages