【问题标题】:Google drive API Python - how to set a destination to files that are downloaded with the apiGoogle drive API Python - 如何为使用 api 下载的文件设置目标
【发布时间】:2020-12-30 21:45:48
【问题描述】:

我正在下载驱动器中文件夹内的文件,并且我想将它们也存储在我的 python 脚本所在位置的本地文件夹中,而不是在没有文件夹的情况下将它们保存为无序,我当前的代码在这里:

import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import io
from googleapiclient.http import MediaIoBaseDownload

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']

def main():
 
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    
    # supose that 213fkrjbk324fnvbknfd is the folder id

    query = "'213fkrjbk324fnvbknfd' in parents"
 
    response = service.files().list(q=query,
                                spaces='drive',
                                fields='files(id, name, parents)').execute()
    
    for document in response['files']:
        #file_id = service.files.list()
        request = service.files().get_media(fileId=document['id'])
        fh = io.FileIO('filename.extension', mode='wb')
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print(document['name'])
            print ("Download %d%%." % int(status.progress() * 100))
            print("-------------------------------------------------------------------------------------")

   

if __name__ == '__main__':
    main()
    
    

当我运行此代码时,控制台显示下载 100% 字符串,但我在脚本位置和本地存储的其他位置都找不到文件。

【问题讨论】:

    标签: python google-api google-drive-api drive


    【解决方案1】:

    下载的数据仍存储在 RAM 中。这会将数据复制到您的 python 文件所在的文件夹中。

    while done is False:
        [....]
    
    # This is where it downloads the file
    fh.seek(0)
    with open('your_filename.pdf', 'wb') as f:
        shutil.copyfileobj(fh, f, length=131072)
    

    如果您遇到问题,我找到了这个答案here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-23
      • 2015-02-06
      • 2020-02-05
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多