4

So I am in the process of trying to upload a file to a specific Google Drive folder that a user specifies. Below is the code I have tried. I get the folderID and I try to post a file to the folder however I get a 404 Not Found error.

def post_file(self, files_to_send, config = {}):
  if config:
    if type(config) is str:
      config = literal_eval(config)
    path_id = config.get('path')
  for file_to_send in files_to_send:
    filename = file_to_send.name.split('/')[-1]
    status = self.call.post('https://www.googleapis.com/upload/drive/v2/files?uploadType=media/' + str(path_id) + "/children")

Using the interface on the Drive API documentation, I am able to get data with the same folderID but I am not able to post (Even with the interface). Files_to_send is a list with all the files that I need to upload to the specific folder, and config contains the folder path.

I need help understanding what I am doing wrong. I have read the documentation and looked at other answer but I have not found a solution. When I don't specify the folder ID, a dummy file does get posted to my drive, but it doesn't upload the file I want, and it doesn't upload anything at all when I specifiy a folderID.

Essentially, I need help inserting a file that comes in to a specific folder in Google Drive.

Thanks!

5 Answers 5

7

in v3 just update file_metadata :

file_metadata = {'name': 'exmample_file.pdf', "parents": ["ID_OF_THE_FOLDER"]}

Sign up to request clarification or add additional context in comments.

Comments

4

I believe you should send a metadata JSON including a parents filed. Something like:

{
    "parents" : [path_id]
}

You can try this API here: https://developers.google.com/drive/v2/reference/files/insert

8 Comments

I tried using the API however I still get a '404 Not Found'. This is the response below: "code": 404, "message": "File not found: "
What is the exact URL you're using?
What do you mean exact URL? Do you mean the ID of the folder?
You seem to be sending a POST request to 'googleapis.com/upload/drive/v2/files?uploadType=media' + str(path_id) + "/children" What exactly does the URL look like after this string is concatenated?
googleapis.com/upload/drive/v2/files?uploadType=media/… That is an example of what the URL looks like going into the POST
|
3

Two steps are required to resolve this issue:

  1. Get folder id:

    folder_list = self.drive.ListFile({'q': "trashed=false"}).GetList()
        for folder in folder_list:
             print 'folder title: %s, id: %s' % (folder['title'], folder['id'])
    
  2. After found the ID Of required folder, Use this func:

    def upload_file_to_specific_folder(self, folder_id, file_name):
        file_metadata = {'title': file_name, "parents": [{"id": folder_id, "kind": "drive#childList"}]}
        folder = self.drive.CreateFile(file_metadata)
        folder.SetContentFile(file_name) #The contents of the file
        folder.Upload()
    

Comments

3

for a concret sample

...
from googleapiclient import discovery
from apiclient.http import MediaFileUpload
...
drive_service = discovery.build('drive', 'v3', credentials=creds)
...
def uploadFile(filename, filepath, mimetype, folderid):
    file_metadata = {'name': filename,
                     "parents": [folderid]
                     }
    media = MediaFileUpload(filepath,
                            mimetype=mimetype)
    file = drive_service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
    print('File ID: %s' % file.get('id'))
  
#sample to use it:
filename = 'cible.png'
filepath = 'path/to/your/cible.png'
mimetype = 'image/png'
folderid = 'your folder id' #ex:'1tQ63546ñlkmulSaZtz5yeU5YTSFMRRnJz'
uploadFile(filename, filepath, mimetype, folderid)

1 Comment

worked , very Good
1

The way I achieve to put the file in the specific folder was by passing below info in metadata.

Here <someFolderID> info can be retrieve as: 1. go to google drive 2. select the folder 3. observe the address bar it will have some value after https://drive.google.com/drive/folders/

{
"parents":[
"id": "<someFolderID>",
"kind": "drive#file"
]
}

1 Comment

"parents":[] should have a enclosing {} within. ie. "parents":[{...}].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.