【问题标题】:problem when uploading file to google drive with its API with Python使用 Python 的 API 将文件上传到谷歌驱动器时出现问题
【发布时间】:2019-05-13 14:58:02
【问题描述】:

我正在尝试使用其 Python API 将文件上传到 Google Drive,因为如果用户交互,我需要制作一个脚本来将自动备份副本从我的服务器上传到 Google Drive。我有以下从 Google Drive 文档中提取的代码。

我的脚本代码:

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

它显示给我的错误是:

Traceback (most recent call last):
  File "gdriveprueba.py", line 55, in <module>
    resumable=True)
  File "/home/servicioweb/.local/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/servicioweb/.local/lib/python2.7/site-packages/googleapiclient/http.py", line 554, in __init__
    fd = open(self._filename, 'rb')
IOError: [Errno 2] No such file or directory: 'files/report.csv'

文件目录在 Google Drive 中手动创建,但它一直告诉我找不到它,我看不到会发生什么?我有 2 天的时间,我无法从脚本上传文件。

【问题讨论】:

  • 错误提示在您的本地驱动器上找不到“files/report.csv”。
  • 你是对的。但现在它向我显示了这个错误: NameError: name 'drive_service' is not defined,我几乎可以肯定我需要导入一些东西,但我不知道它是什么,因为在 Google API 中我找不到任何关于它的信息文件上传部分。
  • 对。此变量未在您的代码中定义。返回您的代码所基于的示例并仔细查看。

标签: python google-drive-api


【解决方案1】:

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

如果您只想将其上传到驱动器的根目录,您可以创建文件files/report.csv,相对于该文件,您将在驱动器的根目录创建文件report.csv

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

要查找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来创建文件。

media = MediaFileUpload('report.csv',
                        mimetype='text/csv',
                        resumable=True)
meta_data= { 'name': 'report.csv',
             'mimeType' : 'application/vnd.google-apps.spreadsheet',
             'parents': [parent_id] }
f = service.files().create(
    body=meta_data,
    media_body=media,
    fields='id').execute()
if not f is None: print("[*] uploaded %s" % (f.get('id')))

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

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多