使用Python的API将文件上载到google驱动器时出现问题

2021-03-01 02:40:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用其pythonapi将文件上载到googledrive,因为我需要创建一个脚本,以便在用户交互时将自动备份副本从服务器上载到googledrive。我有以下代码,我已经从谷歌驱动器文档中提取。在

我的脚本代码:

from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from apiclient.http import MediaFileUpload # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'] def main(): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ 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() # 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 results = service.files().list( pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(u'{0} ({1})'.format(item['name'], item['id'])) file_metadata = { 'name' : 'report.csv', 'mimeType' : 'application/vnd.google-apps.spreadsheet' } media = MediaFileUpload('files/report.csv', mimetype='text/csv', resumable=True) file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() print ("File ID: %s" % file.get("id")) main()

它显示的错误如下:

files目录在googledrive中手动创建它,但它一直告诉我找不到它,可能发生什么我看不到的事情?我有2天在这,我不能上传文件从脚本。在

1条回答
网友
1楼 ·

您混淆了50行和53行上的参数。位于file_metadata结构中的参数name是指google驱动器上的文件名。MediaFileUpload构造函数的第一个参数引用本地驱动器上的路径。要使代码正常工作,此文件必须存在。您还引用了第56行上的未定义变量drive_service。您可以将在main函数中定义的变量service重新定义为global variable,或者将请求api上传的代码(从49开始)移动到函数main中。在上载代码之前,还需要先调用main,才能实际创建服务对象。在

如果您只想将此文件上载到驱动器的根目录下,您只需创建与此文件相关的文件files/report.csv,您将在驱动器的根目录上创建文件report.csv。在

要创建文件files/report.csv,您需要在google驱动器上找到目录filesfileId,并将其作为参数发送给createapi调用。在

要查找fileId,请运行以下代码:

dirp = "files" # Name of directory to find.
parent_id = "" # The id we are looking for.
query = ("name='%s'" % (dirp))
resp = service.files().list(
    q=query,
    fields="files(id, name)",
    pageToken=None).execute()
files = resp.get('files', [])
if len(files) > 0:
    parent_id = files[0].get('id')

现在使用api请求中的变量parent_id来创建文件。在

^{pr2}$

Here是关于create函数参数的更多信息。在

工作代码如下:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient.http import MediaFileUpload

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

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    global service
    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()
        # 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
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

main()

# Retrieve the parent ID of the files/ directory
dirp = "files" # Name of directory to find.
parent_id = "" # The id we are looking for.
query = ("name='%s'" % (dirp))
resp = service.files().list(
    q=query,
    fields="files(id, name)",
    pageToken=None).execute()
files = resp.get('files', [])

# Create a file object for file 'report.csv' on your local drive.
media = MediaFileUpload('report.csv',
                        mimetype='text/csv',
                        resumable=True)

# Upload the file.
if len(files) > 0:
    parent_id = files[0].get('id')
    meta_data= { 'name': 'report.csv',
                 'parents': [parent_id],
                 'mimeType' : 'application/vnd.google-apps.spreadsheet' }
    f = service.files().create(
        body=meta_data,
        media_body=media,
        fields='id').execute()
    if not f is None: print("[*] uploaded %s" % (f.get('id')))
else: print("The folder files/ does not exist on your drive.")

相关问题