我正在使用 Google Drive 作为各种脚本存储常用文件的中心位置。但是,我注意到尽管假装像文件系统一样运行,但 Google Drive 并没有遵守正常的文件系统约定,例如强制使用唯一的文件名。这允许不同的用户上传具有相同文件名的单独文件。
有什么办法可以防止这种情况发生吗?
我的脚本都使用相同的代码来访问 Drive API 以上传文件:
import os
import httplib2
from apiclient import discovery, errors
from apiclient.http import MediaFileUpload
credentials = get_google_drive_credentials()
service = discovery.build('drive', 'v2', http=credentials.authorize(httplib2.Http()))
dir_id = 'google_drive_folder_hash'
filename = '/some/path/blah.txt'
service.files().insert(
body={
'title': os.path.split(filename)[-1],
'parents': [{'id': dir_id}],
},
media_body=MediaFileUpload(filename, resumable=True),
).execute()
如果两个或更多脚本运行此脚本,并假设没有竞争条件,为什么此代码会导致“blah.txt”的重复上传?我将如何防止这种情况?