6
votes

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

3 Answers

20
votes

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']
1
votes

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

0
votes
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)