9

From this example. Can I use MediafileUpload with creating folder? How can I get the parent_id from?

From https://developers.google.com/drive/folder

I just know that i should use mime = "application/vnd.google-apps.folder" but how do I implement this tutorial to programming in Python?

Thank you for your suggestions.

3 Answers 3

23

To create a folder on Drive, try:

 def createRemoteFolder(self, folderName, parentID = None): # Create a folder on Drive, returns the newely created folders ID body = { 'title': folderName, 'mimeType': "application/vnd.google-apps.folder" } if parentID: body['parents'] = [{'id': parentID}] root_folder = drive_service.files().insert(body = body).execute() return root_folder['id'] 

You only need a parent ID here if you want to create folder within another folder, otherwise just don't pass any value for that.

If you want the parent ID, you'll need to write a method to search Drive for folders with that parent name in that location (do a list() call) and then get the ID of that folder.


Edit: Note that v3 of the API uses a list for the 'parents' field, instead of a dictionary. Also, the 'title' field changed to 'name', and the insert() method changed to create(). The code from above would change to the following for v3:

 def createRemoteFolder(self, folderName, parentID = None): # Create a folder on Drive, returns the newely created folders ID body = { 'name': folderName, 'mimeType': "application/vnd.google-apps.folder" } if parentID: body['parents'] = [parentID] root_folder = drive_service.files().create(body = body).execute() return root_folder['id'] 
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! But it's root_folder = drive_service.files().create(body = body).execute() in v3, now at least :)
1

The mediafile uplaod is needed only if you want to insert content. Since you want only to insert metadata (folders are only metadata), you don't need it. A regular POST with the JSON representing the foder is enough.

You can get the parent ID in several ways :

  • searching (file.list end point)
  • inserting folder : this returns you a JSON representing the inserted folder, containing its ID
  • getting it yourself via the web UI (the ID is contained in the URL of your folder or file) : go to the Web UI, select the folder or file you want, then you can identify the fileId in the URL. ex : https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ
    The file Id is the last part of the URL, ie. 0B8VrsrGIcVbrRDVxMXFWVkdfejQ

How to get an FileID programatically :

  1. Use the children.list endpoint using a known fileId to get the ids of the children of this known ID.
  2. Use the search feature of google drive : files.list endpoint with a q parameter
  3. Use aliases : the only one I know in Google Drive is root for the root folder of your Drive.

Using 3. and 1., you can get all the fileIds of your Drive.

I dont know how I can be clearer

4 Comments

Oh thannks Jerome but now i still misunderstand .I'm just the beginner for programming google drive , Could I ask more question--- How to retrieve both of FileId and ParentId such as example code from here developers.google.com/drive/v2/reference/files/get where can i get fileId to use that code?
For testing purpose, e.g. using the form in the link you provided, go to the Web UI, select the folder or file you want, then you can identify the fileId in the URL. ex : https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ
and How I get it from URL ? Does it have its command or method to get the url that i can split fileid from it?
please explain some more detail using your 3rd choice. I don't be too cleary. So Thank you @Jerome .Your suggestion help me very much
0
def create_folder(header, folder_name, folder_id, drive_id=None): url = 'https://www.googleapis.com/upload/drive/v3/files' file_metadata = { 'name': folder_name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [folder_id] } file_withmetadata = {"data": ("metadata", json.dumps(file_metadata), "application/json; charset=UTF-8")} param = {"q":"'%s' in parents" %folder_id, "supportsAllDrives":"true"} if drive_id is not None: param['driveId'] = drive_id r = requests.post( url, headers=header, params=param, files=file_withmetadata ) return json.loads(r.text) 

1 Comment

It might be better to put a little explanation for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.