简体   繁体   English

如何在 Python 中使用 Google Drive API 创建新文件夹?

[英]How can I create a new folder with Google Drive API in Python?

From this example .这个例子 Can I use MediafileUpload with creating folder?我可以在创建文件夹时使用 MediafileUpload 吗? How can I get the parent_id from?我怎样才能得到 parent_id 从?

From https://developers.google.com/drive/folder来自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?我只知道我应该使用 mime = "application/vnd.google-apps.folder" 但我如何实施本教程以使用 Python 进行编程?

Thank you for your suggestions.谢谢你的建议。

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.如果你想在另一个文件夹中创建文件夹,你只需要一个父 ID,否则不要为此传递任何值。

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.如果您想要父 ID,则需要编写一种方法来在云端硬盘中搜索该位置中具有该父名称的文件夹(执行 list() 调用),然后获取该文件夹的 ID。


Edit: Note that v3 of the API uses a list for the 'parents' field, instead of a dictionary.编辑:请注意,API 的 v3 使用列表作为“父母”字段,而不是字典。 Also, the 'title' field changed to 'name' , and the insert() method changed to create() .此外, 'title'字段更改为'name'insert()方法更改为create() The code from above would change to the following for v3:对于 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']

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.一个带有 JSON 的常规 POST 表示 foder 就足够了。

You can get the parent ID in several ways:您可以通过多种方式获取父 ID:

  • searching (file.list end point)搜索(file.list 终点)
  • inserting folder: this returns you a JSON representing the inserted folder, containing its ID插入文件夹:这会返回一个表示插入文件夹的 JSON,其中包含其 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.通过Web UI 自行获取(ID 包含在您的文件夹或文件的URL 中):进入Web UI,选择您想要的文件夹或文件,然后您可以识别URL 中的fileId。 ex: https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ例如: https://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ ://drive.google.com/#folders/0B8VrsrGIcVbrRDVxMXFWVkdfejQ
    The file Id is the last part of the URL, ie.文件 ID 是 URL 的最后一部分,即。 0B8VrsrGIcVbrRDVxMXFWVkdfejQ

How to get an FileID programatically:如何以编程方式获取 FileID:

  1. Use the children.list endpoint using a known fileId to get the ids of the children of this known ID.使用已知 fileId 的 children.list 端点来获取此已知 ID 的子项的 ID。
  2. Use the search feature of google drive: files.list endpoint with aq parameter使用 google drive 的搜索功能:带有 aq 参数的 files.list 端点
  3. Use aliases: the only one I know in Google Drive is root for the root folder of your Drive.使用别名:我在 Google 云端硬盘中唯一知道的别名是root代表您的云端硬盘的根文件夹。

Using 3. and 1., you can get all the fileIds of your Drive.使用 3. 和 1.,您可以获得驱动器的所有 fileId。

I dont know how I can be clearer我不知道怎样才能更清楚

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用rest api在谷歌驱动器中创建新文件夹 - create new folder in google drive with rest api 如何通过python Google drive API创建新文件夹,MediaFileUpload的用途是什么? - How to create a new folder by python google drive api and what is the use of MediaFileUpload? 我可以使用 Python 服务帐户创建 Google Drive 文件夹吗? - Can I create a Google Drive folder using service account with Python? 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python 如何使用 Google 的 Python Drive Client API 在 Team Drive 中创建文件夹? - How to create a folder in Team Drive with Google's Python Drive Client API? 新的 Google Drive API - 如何创建快捷方式? - New Google Drive API - how to create shortcut? Google Drive 在共享云端硬盘中创建新文件夹 - Google Drive create new folder in shared drive Google Drive API for Python:如何创建凭证? - Google Drive API for Python: how to create credential? 如何在没有驱动器 API 的情况下使用 selenium 和 python 登录谷歌驱动器 - How can I login to google drive using selenium & python WITHOUT Drive API 如何使用 Google Drive API 和 Python 将文件上传到特定文件夹? - How to upload file into specific folder with Google Drive API and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM