проблема при загрузке файла на гугл диск с его API с Python - PullRequest
0 голосов
/ 13 мая 2019

Я пытаюсь загрузить файл на Google Drive, используя его API-интерфейс Python, поскольку мне нужно создать сценарий для автоматической загрузки резервных копий с моего сервера на Google Drive при взаимодействии с пользователем.У меня есть следующий код, который я извлек из документации Google Диска.

Код моего скрипта:

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 Диске, но он постоянно говорит мне, что не может его найти, что может случиться, чего я не вижу?У меня есть 2 дня, и я не смог загрузить файлы из скрипта.

1 Ответ

0 голосов
/ 22 мая 2019

Вы путаете параметры в строке 50 и строке 53. Параметр name, который входит в структуру file_metadata, ссылается на имя файла на диске Google . Первый параметр конструктора MediaFileUpload относится к пути на локальном диске . Чтобы ваш код работал, этот файл должен существовать. Также вы ссылаетесь на неопределенную переменную drive_service в строке 56. Вы можете либо переопределить переменную service, которая определена в основной функции, как глобальную переменную , либо переместить код, который запрашивает загрузку API (начиная со строки 49), в функцию main. Кроме того, main необходимо сначала вызвать перед загрузкой кода, чтобы фактически создать объект службы.

Если вы просто хотите загрузить это в корень вашего диска, вы можете просто создать файл files/report.csv относительно этого файла, и у вас будет файл report.csv, созданный в корне вашего диска.

Чтобы создать файл files/report.csv, вам нужно найти fileId каталога files на вашем диске Google и отправить его в качестве параметра для вызова 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')

Теперь используйте переменную parent_id в запросе API для создания файла.

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

Здесь - дополнительная информация о параметрах для функции 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.")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...