早上好,
我使用Python脚本在Google云端硬盘上传文件。
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaFileUpload
from apiclient import errors
import io
from apiclient.http import MediaIoBaseDownload
folder_id = 'MyID021847773466366'
try :
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
print("make new storage data file ")
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
flow.redirect_uri = client.OOB_CALLBACK_URN
authorize_url = flow.step1_get_authorize_url()
creds = tools.run_flow(flow, store, flags) \
if flags else tools.run(flow, store)
DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
FILES = (
('CVE.xlsx'),
)
folder_id = 'ds8d7s8d78s7d8s7d8'
file_metadata = {
'name': 'CVE.xlsx',
'parents': [folder_id],
'mimeType': None
}
media = MediaFileUpload('CVE.xlsx',
mimetype=None,
resumable=True)
file = DRIVE.files().insert(body=file_metadata , media_body=media,fields='id').execute()
它工作得很好wie" V2"。如果我使用Google-Spredsheats打开上传的文件,我会收到第二个文件:
CSV.xlsx -- 5MB (uploaded file)
CSV.xlsx -- -- (new file)
我的问题:我想在线修改文件并在我的机器上自动下载:
file_id = 'd56d5d5d5d5d5d'
request = DRIVE.files().get_media(fileId=file_id)
fh = io.FileIO('CVE.xlsx', 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
我还有一个代码可以在我的机器上通过过滤器修改后上传它:
file_id = 'd56d5d5d5d5d5d'
file = DRIVE.files().get(fileId=file_id).execute()
updated_file = DRIVE.files().update(
fileId=file_id,
body=file,
media_body=media).execute()
但这仅适用于原始文件的file_id(上传的5MB文件)。只要我使用Google电子表格打开文件,它就无效。
下载代码的错误消息:
"回溯(最近一次呼叫最后一次):文件" pydri.py",第81行,在 status,done = downloader.next_chunk()文件" C:\ Python27 \ lib \ site-packages \ oauth2client_helpers.py",第133行,in po sitional_wrapper return wrapped(* args,** kwargs)File" C:\ Python27 \ lib \ site-packages \ googleapiclient \ http.py",第692行,in nex t_chunk 引发HttpError(resp,content,uri = self._uri)googleapiclient.errors.HttpError:https://www.goo gleapis.com/drive/v2/files/1_xJVvq3-hOGPPUJ4T3B8_Dz?alt=medi a 返回"找不到档案:1_xJVvq3-hOGPPUJ4T3B8_Dz">"
有人能帮助我吗?