How do I create a new folder with the Google Drive API in Python?

From this example . Can MediafileUpload be used with folder creation? How can I get parent_id?

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

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

Thanks for your suggestions.

+5


source to share


2 answers


To create a folder in 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']

      

Here you only need the parent id if you want to create a folder in another folder, otherwise just don't pass any value for that.

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




Edit: Please note that the v3 API uses a list for the parent field instead of a dictionary. In addition, the field 'title'

changed to 'name'

, and the method insert()

changed to create()

. The code above would change to 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']

      

+17


source


The uplaod media is only needed if you want to embed content. Since you only want to embed metadata (folders are just metadata), you don't need that. A regular POST with JSON representing the foder is sufficient.

You can get the parent id in several ways:

  • search (endpoint of .list file)
  • inserting folder: this returns JSON representing the inserted folder containing its id
  • get it through the web interface (the id is contained in the url of your folder or file): go to the web interface, select the folder or file you want, then you can define the file in the url. ex: https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ


    The file id is the last part of the url, i.e.0B8VrsrGIcVbrRDVxMXFWVkdfejQ




How to get a file id programmatically:

  • Use the children.list endpoint with a known file to get the child ids of that known ID.
  • Use google drive endpoint search function: files.list with q parameter
  • Use Aliases: The only one I know in Google Drive root

    for the root folder of your Drive.

Using 3 and 1., you can get all the files on your Drive.

I don't know how I can be clearer

+1


source







All Articles