Skip to content Skip to sidebar Skip to footer

Impossible To Upload Pdf On Gdrive Subfolders With Python Using Service Account

I created a flask project which stores a pdf on google drive. The user simpliy drags a pdf over a DIV, and flask uploads the file to Drive using a service account. The upload works

Solution 1:

One issue is fixed, I had simply to log to the admin console and follow the instructions described in the service account page "Delegating domain-wide authority to the service account".

Then I selected the scopes according to the page About authorization

I have still problems when I try to load a file into a subfolder, the file goes into root folder

Solution 2:

Ok I understood how to load files into subfolders too. The 'parents' metadata propety takes a list of folders, it means that a file will be uploaded in all the folders of the list at the same time.

The files are created with the same ID, so when I run drive_service.files().delete(fileId=id_file).execute() the file will be removed from every folder it was uploaded to.

In another thread I read that it's necessary to provide the "kind" property too, something like that:

[{"id": "XXXXXXXXXXXXXXXXXXX","kind": "drive#file"},{"id":"YYYYYYYYYYYYYYYYY","kind": "drive#file"}]

but it didn't work for me, but probably I misunderstood. A simple list object with the IDs of the folder is required, as shown below.

folder_id = [folder1, folder2, folder3]


body = {
    'name': filename,
    'mimeType': mime_type,
    'useDomainAdminAccess':True,
    'parents':folder_id
}

Hope this will help somenone who uses Google Drive API for the first time.

Fabio

Post a Comment for "Impossible To Upload Pdf On Gdrive Subfolders With Python Using Service Account"