我想将文件上传到GDrive,并为拥有该链接的所有用户获取下载链接。目前,我设法upload files获得了download link。但我不知道如何获取所有用户的下载链接。有人可以帮帮我吗?
答案 0 :(得分:1)
此documentation中说明Google云端硬盘支持通过webViewLink
媒体资源为用户提供对文件的直接访问权限。
此外,Google云端硬盘为您提供了三种下载文件的方式。
下载文件 - files.get with alt = media file resource
下载并导出Google文档 - files.export
- 的webContentLink
将用户链接到文件 - 来自file resource
请记住,当您使用webContentLink
下载文件时,这仅适用于云端硬盘中包含二进制内容的文件。
要获取Google云端硬盘文件的直接下载链接,您可以转到Google云端硬盘开发者API参考,并使用他们的 “try it!”
API资源管理器表单get
API调用。在fileId
文本框中,只需粘贴file’s ID
并在fields
文本框中,只需放置webContentLink
,然后点击 “Execute”
即可。在响应下面,在生成的JSON中,您需要的直接链接称为webContentLink
,看起来像这样:
"webContentLink": "https://docs.google.com/uc?id=0ByP7_A9vXm17TmRYcmNScnYzS1E&export=download"
有关详细信息,请查看此thread。
答案 1 :(得分:0)
您必须编辑该文件的权限,以使其可供所有用户访问。
代码:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import multiprocessing as mp
import os
gauth = GoogleAuth()
# Creates local webserver and auto handles authentication.
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
#if there is any error in clent_secret.json try downloading it again after enabling drive api
#now the drive is accessible through python
#Lets create a folder named 'New_folder' in gdrive
file1 = drive.CreateFile({'title' : 'New_folder',
'mimeType': "application/vnd.google-apps.folder"})
file1.Upload()
#it will create a folder in your drive
#mimeType is an argument which specifies the type of file
#Now we have created a folder, lets see how to upload a file to the folder which we have created
#We need to obtain the Id of the folder which we have created in the previous steps
folder_id = file1['id']
#lets upload an image to the folder
file2 = drive.CreateFile({'title':'filename.jpg',
'mimeType':'image/jpeg',
'parents': [{"kind": "drive#fileLink", "id": folder_id}]
#In parents argument above we need to specify the folder ID of the folder to which it has to be uploaded.
#mimeType is different for each file type, which are available in the google api documentation.
#specify the local path with the quotes in the below line
file2.SetContentFile('/home/username/Downloads/image.jpg')
file2.Upload()
#SET PERMISSION
permission = file2.InsertPermission({
'type': 'anyone',
'value': 'anyone',
'role': 'reader'})
#SHARABLE LINK
link=file2['alternateLink']
#To use the image in Gsheet we need to modify the link as follows
link=file2['alternateLink']
link=link.split('?')[0]
link=link.split('/')[-2]
link='https://docs.google.com/uc?export=download&id='+link
print link