需求:内网服务器没有图形界面时,Google Drive作为中转站传输数据使用,使用scp等工具特别慢
上传文件
Google OAuth 客户端凭据 (Client Credentials)
Thanks gdrive !
https://github.com/glotlabs/gdrive/blob/main/docs/create_google_api_credentials.md
Create Google API credentials in 50 easy steps
Google has made it really easy to create api credentials for own use, just follow these few steps:
Go to Google Cloud Console
Create a new project (or select an existing) from the menu
Search for
drive api
in the search bar and selectGoogle drive api
under the marketplace sectionClick to enable
Google Drive API
buttonClick on the
Credentials
menu itemClick on the
Configure Consent Screen
buttonSelect
External
user type (Internal is only available for workspace subscribers)Click on the
Create
buttonFill out the fields
App name
,User support email
,Developer contact information
with your information; you will need to put the Project ID into the app name (keep the other fields empty)Click the
Save and continue
button. If you getAn error saving your app has occurred
try changing the project name to something uniqueClick the
Add or remove scopes
buttonSearch for
google drive api
Select the scopes
.../auth/drive
and.../auth/drive.metadata.readonly
Click the
Update
buttonClick the
Save and continue
buttonClick the
Add users
buttonAdd the email of the user you will use with gdrive
Click the
Add
button until the sidebar disappearsClick the
Save and continue
buttonClick on the
Credentials
menu item againClick on the
Create credentials
button in the top bar and selectOAuth client ID
Select application type
Desktop app
and give a nameClick on the
Create
buttonYou should be presented with a Cliend Id and Client Secret
Click on
OAuth consent screen
Click on
Publish app
(to prevent the token from expiring after 7 days)Click
Confirm
in the dialog
Thats it!
Gdrive will ask for your Client Id and Client Secret when using the gdrive account add
command.
使用curl上传文件
之前创建的OAuth是桌面应用,这里需要更改成TV和其他限制输入设备
验证设备
1
curl -d "client_id=<client_id>&scope=https://www.googleapis.com/auth/drive.file" https://oauth2.googleapis.com/device/code
得到如下的回复:
1 2 3 4 5 6 7
{ "device_code": "<long string>", "user_code": "xxx-xxx-xxx", "expires_in": 1800, "interval": 5, "verification_url": "https://www.google.com/device" }
这里我们需要访问网址(https://www.google.com/device)并提供用户代码来完成我们的验证。我们现在继续选择我们的谷歌账户并授予相关权限。执行此操作时,请务必记下下一步的设备代码。
得到Bearer code
当我们开始上传时,这是我们需要用来识别我们帐户的代码。我们通过使用以下方法获得它:
1
curl -d client_id=<client id> -d client_secret=<client secret> -d device_code=<device code> -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token
client id 和 secret 是从第一步保存的,以及上一节中的设备代码。输出应采用以下格式:
1 2 3 4 5 6 7
{ "access_token": ".....", "expires_in": 3599, "refresh_token": "....", "scope": "https://www.googleapis.com/auth/drive.file", "token_type": "Bearer" }
记下上传阶段需要的 access_token
上传文件
1 2 3 4 5
curl -X POST -L \ -H "Authorization: Bearer <enter access token here>" \ -F "metadata={name :'<our.zip>'};type=application/json;charset=UTF-8" \ -F "file=@<our.zip>;type=application/zip" \ "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
使用Python
现在我们知道我们的命令有效,我们可以创建一个可执行脚本来为我们完成所有工作。在这里我们可以提供一组文件,它将它们压缩,然后将它们发送到谷歌驱动器。
我们首先使用 nano curlgoogle 创建一个新文件;并输入以下代码——记得添加您自己的个人身份验证令牌!已选择 Python 2.7,因为这仍然是旧系统上的默认 python 版本,但是下面的脚本也应该适用于 python 3。 如果系统上已经存在 curl,它应该不需要新的依赖项。
|
|
|
|
下载文件
在 Google Drive 将文件设置为共享,得到如下链接
https://drive.google.com/file/d/1Cy_MpY452A------------xkIRWJhPwz/view?usp=sharing
需要设置下面的文件id和下载的文件名:
import gdown
url = 'https://drive.google.com/uc?id=1Cy_MpY452A------------xkIRWJhPwz'
output = <name>
gdown.download(url, output, quiet=False)