【问题标题】:Python: How to upload files to a specific folder in Google Drive using folderIdPython:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹
【发布时间】:2014-11-06 15:40:49
【问题描述】:

所以我正在尝试将文件上传到用户指定的特定 Google Drive 文件夹。下面是我尝试过的代码。我得到了 folderID 并尝试将文件发布到文件夹中,但是我收到了 404 Not Found 错误。

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")

使用 Drive API 文档上的接口,我能够获取具有相同文件夹 ID 的数据,但我无法发布(即使使用接口)。 Files_to_send 是一个列表,其中包含我需要上传到特定文件夹的所有文件,config 包含文件夹路径。

我需要帮助来了解我做错了什么。我已经阅读了文档并查看了其他答案,但我还没有找到解决方案。当我没有指定文件夹 ID 时,一个虚拟文件确实会发布到我的驱动器,但它不会上传我想要的文件,并且当我指定文件夹 ID 时它根本不会上传任何东西。

基本上,我需要帮助插入进入 Google Drive 中特定文件夹的文件。

谢谢!

【问题讨论】:

    标签: python google-drive-api


    【解决方案1】:

    我认为您应该发送包含parents 归档的元数据 JSON。 比如:

    {
        "parents" : [path_id]
    }
    

    您可以在此处试用此 API:https://developers.google.com/drive/v2/reference/files/insert

    【讨论】:

    【解决方案2】:

    解决此问题需要两个步骤:

    1. 获取文件夹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. 找到所需文件夹的ID后,使用这个函数:

      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()
      

    【讨论】:

      【解决方案3】:

      在 v3 中只更新 file_metadata :

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

      【讨论】:

        【解决方案4】:

        我将文件放入特定文件夹的方法是在元数据中传递以下信息。

        这里的<someFolderID> 信息可以检索为: 1.去谷歌驱动 2.选择文件夹 3.观察地址栏https://drive.google.com/drive/folders/后会有一些值

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

        【讨论】:

        • "parents":[] 应该有一个封闭的{}。 IE。 "parents":[{...}].
        【解决方案5】:

        混凝土样品

        ...
        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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-01-16
          • 2016-07-26
          • 1970-01-01
          • 1970-01-01
          • 2015-10-18
          • 1970-01-01
          • 2019-07-26
          相关资源
          最近更新 更多