我需要從某些Linux服務器備份各種文件類型到GDrive(不只是可轉換爲GDocs格式)。如何使用Python腳本將文件上傳到Google Drive?
什麼是最簡單,最優雅的方式來做到這一點與python腳本?有關GDoc的解決方案是否適用?
我需要從某些Linux服務器備份各種文件類型到GDrive(不只是可轉換爲GDocs格式)。如何使用Python腳本將文件上傳到Google Drive?
什麼是最簡單,最優雅的方式來做到這一點與python腳本?有關GDoc的解決方案是否適用?
您可以使用文檔列表API編寫寫入到驅動器的腳本:
https://developers.google.com/google-apps/documents-list/
無論是文檔列表API和驅動API使用相同的資源(即相同的文檔和文件)交互。
此樣品中的Python客戶端庫展示瞭如何上傳未轉換的文件到驅動器:
http://code.google.com/p/gdata-python-client/source/browse/samples/docs/docs_v3_example.py#180
文檔列表API已於2012年9月14日棄用,應使用Google Drive API代替https://developers.google.com/drive/ –
用於保存文件使用Python Google雲端硬盤當前的文檔可以在這裏找到: https://developers.google.com/drive/v3/web/manage-uploads
但是,谷歌驅動器API處理文檔存儲和檢索的方式並不遵循與POSIX文件系統相同的體系結構。因此,如果您希望在Linux文件系統上保留嵌套文件的分層體系結構,則需要編寫大量自定義代碼,以便將父目錄保存在Google Drive上。
最重要的是,谷歌很難獲得正常的驅動器帳戶的寫入權限。您的權限範圍必須包含以下鏈接:https://www.googleapis.com/auth/drive並且要獲取令牌以訪問用戶的正常帳戶,該用戶必須首先提供join a group以提供對未審覈應用的訪問權限。而且任何創建的oauth標記都具有有限的保質期。
但是,如果您獲得訪問令牌,則以下腳本應允許您將本地計算機上的任何文件保存到谷歌驅動器上的相同(相對)路徑。
def migrate(file_path, access_token, drive_space='drive'):
'''
a method to save a posix file architecture to google drive
NOTE: to write to a google drive account using a non-approved app,
the oauth2 grantee account must also join this google group
https://groups.google.com/forum/#!forum/risky-access-by-unreviewed-apps
:param file_path: string with path to local file
:param access_token: string with oauth2 access token grant to write to google drive
:param drive_space: string with name of space to write to (drive, appDataFolder, photos)
:return: string with id of file on google drive
'''
# construct drive client
import httplib2
from googleapiclient import discovery
from oauth2client.client import AccessTokenCredentials
google_credentials = AccessTokenCredentials(access_token, 'my-user-agent/1.0')
google_http = httplib2.Http()
google_http = google_credentials.authorize(google_http)
google_drive = discovery.build('drive', 'v3', http=google_http)
drive_client = google_drive.files()
# prepare file body
from googleapiclient.http import MediaFileUpload
media_body = MediaFileUpload(filename=file_path, resumable=True)
# determine file modified time
import os
from datetime import datetime
modified_epoch = os.path.getmtime(file_path)
modified_time = datetime.utcfromtimestamp(modified_epoch).isoformat()
# determine path segments
path_segments = file_path.split(os.sep)
# construct upload kwargs
create_kwargs = {
'body': {
'name': path_segments.pop(),
'modifiedTime': modified_time
},
'media_body': media_body,
'fields': 'id'
}
# walk through parent directories
parent_id = ''
if path_segments:
# construct query and creation arguments
walk_folders = True
folder_kwargs = {
'body': {
'name': '',
'mimeType' : 'application/vnd.google-apps.folder'
},
'fields': 'id'
}
query_kwargs = {
'spaces': drive_space,
'fields': 'files(id, parents)'
}
while path_segments:
folder_name = path_segments.pop(0)
folder_kwargs['body']['name'] = folder_name
# search for folder id in existing hierarchy
if walk_folders:
walk_query = "name = '%s'" % folder_name
if parent_id:
walk_query += "and '%s' in parents" % parent_id
query_kwargs['q'] = walk_query
response = drive_client.list(**query_kwargs).execute()
file_list = response.get('files', [])
else:
file_list = []
if file_list:
parent_id = file_list[0].get('id')
# or create folder
# https://developers.google.com/drive/v3/web/folder
else:
if not parent_id:
if drive_space == 'appDataFolder':
folder_kwargs['body']['parents'] = [ drive_space ]
else:
del folder_kwargs['body']['parents']
else:
folder_kwargs['body']['parents'] = [parent_id]
response = drive_client.create(**folder_kwargs).execute()
parent_id = response.get('id')
walk_folders = False
# add parent id to file creation kwargs
if parent_id:
create_kwargs['body']['parents'] = [parent_id]
elif drive_space == 'appDataFolder':
create_kwargs['body']['parents'] = [drive_space]
# send create request
file = drive_client.create(**create_kwargs).execute()
file_id = file.get('id')
return file_id
PS。我已經從labpack
python模塊修改了這個腳本。在該模塊中有一個名爲driveClient的類,該類由rcj1492編寫,它以保留POSIX文件系統的方式處理保存,加載,搜索和刪除谷歌驅動器上的文件。
from labpack.storage.google.drive import driveClient
我發現PyDrive優雅的處理驅動器API,它也有很大的documentation(尤其是走通過認證部分的用戶)。
編輯:再加上上Automating pydrive verification process和Pydrive google drive automate authentication的材料,這使得一些偉大的文檔,得到的東西去。希望它能幫助那些對從哪裏開始感到困惑的人。
什麼是2016上傳到g盤的方式? –