Reputation: 164
I'm trying to upload a local file to a a specified folder in Google Drive, and I'm only successful in getting it uploaded to Drive, not in the folder I'm telling it to. I've tested with a shared folder as well as a non-shared folder and I'm getting the same result. No error, just the file being in the root Drive directory.
creds = GetGoogleCredentials()
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
FileSearch = service.files().list(q="name='{0}'".format(file), fields="nextPageToken, files(id, name)").execute()
items = FileSearch.get('files', [])
FolderID = 'IDHere'
if not items:
file_metadata = {'name': file,
"parents": FolderID
}
media = MediaFileUpload('folder/en/' + file,resumable=True)
service.files().create(body=file_metadata,media_body=media,fields='id').execute()
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
Based off what I've found, the "parents": FolderID
entry should be what I need, but it's like the create
method is ignoring it.
Upvotes: 1
Views: 140
Reputation: 201643
If my understanding is correct, how about this modification? Please modify parents
in the object of file_metadata
as follows.
"parents": FolderID
"parents": [FolderID]
If this was not the direct solution of your issue, I apologize.
Upvotes: 2