如何解决PyDrive:上传的 .mp4 文件无法访问其他用户被 403 禁止
当我从只能访问我的 Weblink API 获取链接时,我有一个脚本用于将 .mp4 文件存储到 Google Drive。我希望必须访问整个域或组织的链接,但它没有发生。我试图插入权限,但得到“文件权限不足”。下面是我的代码实现。
import os
从 pydrive2.auth 导入 GoogleAuth 从 pydrive2.drive 导入 GoogleDrive
GDriveService 类: def init(自我): self.drive = 无 self.gdrive_folder_name = 'Davros-Records'
def davros_authenticate(self):
settings_file_path = os.path.abspath(__file__)
settings_file_path = os.path.join(os.path.split(settings_file_path)[0],"settings.yaml")
creds_file_path = os.path.join(os.path.split(settings_file_path)[0],"mycreds.txt")
gauth = GoogleAuth(settings_file_path)
# Try to load saved client credentials
gauth.LoadCredentialsFile(creds_file_path)
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
self.drive = GoogleDrive(gauth)
def create_folder(self,folder_name):
"""
Method is used to create a new folder in the Google drive returns folder-id.
:param str folder_name: Contains the folder name to be created in the google drive.
"""
folder = self.drive.CreateFile({'title': folder_name,"mimeType": "application/vnd.google-apps.folder"})
folder.Upload()
return folder["id"]
def check_folder_exists(self,folder_name,user,feature):
"""
Method is used to check if a folder exists in the google drive.
:param str folder_name: Contains the folder name to be created in the google drive.
"""
file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
folder_id = None
for file in file_list:
if folder_name == file['title']:
folder_id = file['id']
return folder_id
def upload_file_inside_folder(self,title,fid,file_path):
"""
Method is used for uploading a file inside the specified folder on google drive.
:param str title: uploaded file name in the google drive.
:param str fid: contains folder id.
:param str file_path: Contains the full path of the uploaded file in the client side.
"""
file = self.drive.CreateFile({'title': title,"parents": [{"kind": "drive#fileLink","id": fid}]})
file.SetContentFile(file_path)
file.Upload()
user_permissions = {
'type': 'anyone','value': 'anyone','role': 'reader'
}
file.InsertPermission(user_permissions)
return True
def upload_file_gdrive(self,file_path,feature):
"""
Used to call the methods and upload file to the google drive.
:param str file_path: Contains the full path of the uploaded file in the client side.
"""
file_name = os.path.basename(file_path)
folder_id = self.check_folder_exists(self.gdrive_folder_name,feature)
if folder_id is None:
folder_id = self.create_folder(self.gdrive_folder_name)
self.upload_file_inside_folder(file_name,folder_id,file_path)
gdrive = GDriveService() gdrive.davros_authenticate() gdrive.upload_file_gdrive("C:\Users\srikantp\Downloads\climateV.mp4","srikantp","远程控制")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。