![](/img/trans.png)
[英]Get Google drive shareable link without allowing user to download file
[英]Get a shareable link of a file in our google drive using Colab notebook
誰能告訴我如何使用 Colab notebook 自動獲取我們 google drive 中文件的可共享鏈接?
謝謝你。
您可以使用xattr
獲取 file_id
from subprocess import getoutput
from IPython.display import HTML
from google.colab import drive
drive.mount('/content/drive') # access drive
# need to install xattr
!apt-get install xattr > /dev/null
# get the id
fid = getoutput("xattr -p 'user.drive.id' '/content/drive/My Drive/Colab Notebooks/R.ipynb' ")
# make a link and display it
HTML(f"<a href=https://colab.research.google.com/drive/{fid} target=_blank>notebook</a>")
在這里,我在/Colab Notebooks/R.ipynb
訪問我的筆記本文件,並創建一個鏈接以在 Colab 中打開它。
如果您查看文檔,您會看到解釋如何列出雲端硬盤中的文件的部分。
使用它並閱讀所用庫的文檔,我創建了這個腳本:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
files = drive.ListFile().GetList()
for file in files:
keys = file.keys()
if 'webContentLink' in keys:
link = file['webContentLink']
elif 'webViewLink' in keys:
link = file['webViewLink']
else:
link = 'No Link Available. Check your sharing settings.'
if 'name' in keys:
name = file['name']
else:
name = file['id']
print('name: {} link: {}'.format(name, link))
這當前列出了所有文件並提供了指向它的鏈接。
然后,您可以編輯 function 以查找特定文件。
希望這可以幫助!
就我而言,建議的解決方案不起作用。 所以我用“https://drive.google.com//file/d/”替換了colab URL
在我使用的下面:
def get_shareable_link(file_path):
fid = getoutput("xattr -p 'user.drive.id' " + "'" + file_path + "'")
print(fid)
# make a link and display it
return HTML(f"<a href=https://drive.google.com/file/d/{fid} target=_blank>file URL</a>")
get_shareable_link("/content/drive/MyDrive/../img_01.jpg")
get_output 沒有在其他答案中為我定義,但是在執行 drive.mount 命令后,它起作用了:(test.zip 需要指向您的驅動器中的正確文件夾和文件:):
!apt-get install -qq xattr
filename = "/content/drive/My\ Drive/test.zip"
# Retrieving the file ID for a file in `"/content/drive/My Drive/"`:
id = !xattr -p 'user.drive.id' {filename}
print(id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.