簡體   English   中英

Google Cloud Storage - 使用 CURL 請求下載文件

[英]Google Cloud Storage - Downloading a file using CURL request

我正在嘗試使用 curl 請求從雲存儲桶下載文件,如此處所述 - https://cloud.google.com/storage/docs/downloading-objects#download-object-json

在上面的文檔中,在第 1 步中,我看到訪問令牌是從Oauth 2.0 游樂場生成的。 但是,我想以編程方式生成令牌並發送 CURL 請求。 \

有沒有辦法通過任何腳本獲取訪問令牌? 可能來自另一個使用服務帳戶的 CURL 請求?

將 OAuth 2.0 用於服務器到服務器應用程序

Bash OAuth 2.0 JWT 服務器到谷歌服務器應用程序的腳本

下載對象

#1. Create a service account with storage permissions to download the objects and download the p12 key file


#2. Convert p12 key to pem
PRIVATE_KEY=privateKey.pem
openssl pkcs12 -in privatekey.p12 -nodes -nocerts --passin pass:notasecret > $PRIVATE_KEY


#3. Create an object and uploaded to storage
cat file
#This is a testing file
gsutil cp file gs://your-bucket/


#4.Create a JSON Web Token (JWT, pronounced, "jot") which includes a header, a claim set, and a signature.

ALG=RS256
TYP=JWT

JSON_HEADER=$( jq -n --arg alg "$ALG" --arg typ "$TYP"  '{alg: $alg, typ: $typ}' )
JSON_HEADER_ENCODED=`echo -n $JSON_HEADER | openssl base64 -e`

ISS=user@project.iam.gserviceaccount.com
SCOPE=https://www.googleapis.com/auth/cloud-platform
AUD=https://oauth2.googleapis.com/token
IAT=$(date +%s)
EXP=$(($IAT + 3600))



JSON_CLAIM=$( jq -n --arg iss "$ISS" --arg scope "$SCOPE" --arg aud "$AUD" --arg exp "$EXP" --arg iat "$IAT"  '{iss: $iss, scope: $scope, aud: $aud, exp: $exp, iat: $iat}');
JSON_CLAIM_ENCODED=`echo -n $JSON_CLAIM | openssl base64 -e`


HEAD_AND_CLAIM_TR=`echo -n "$JSON_HEADER_ENCODED.$JSON_CLAIM_ENCODED" | tr -d '\n' | tr -d '=' | tr '/+' '_-'`
echo $HEAD_AND_CLAIM_TR

SIGNATURE_ENCODED=`echo -n "$HEAD_AND_CLAIM_TR" | openssl dgst -sha256 -sign $PRIVATE_KEY | openssl base64 -e`
SIGNATURE_TR=`echo -n "$SIGNATURE_ENCODED" | tr -d '\n' | tr -d '=' | tr '/+' '_-'`
echo $SIGNATURE_TR


JWT_ASSERTION="$HEAD_AND_CLAIM_TR.$SIGNATURE_TR"
echo $JWT_ASSERTION


#5. Request an access token from the Google OAuth 2.0 Authorization Server.
RESPONSE=`curl -H "Content-type: application/x-www-form-urlencoded" -X POST "https://oauth2.googleapis.com/token" -d \
"grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=$JWT_ASSERTION" `


#6. Handle the JSON response that the Authorization Server returns.
BEARER=`echo $RESPONSE | jq '.access_token'`


#7. Download the object from GCS
curl -X GET \
  -H "Authorization: Bearer $BEARER" \
  -o "test_file" \
  "https://storage.googleapis.com/storage/v1/b/your-bucket/o/file?alt=media" 
 
  
#8. Test it
cat test_file
#This is a testing file

使用 CURL,您可以使用此命令gcloud auth print-access-token 為此,您需要使用用戶憑據gcloud auth login進行身份驗證

如果您只有一個服務帳戶密鑰文件(因為您的腳本不在本地或 Google Cloud 環境中運行),您可以像這樣加載憑據: gcloud auth activate-service-account --key-file=<YOUR FILE>

然后,您的 CURL 命令如下所示

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -o "SAVE_TO_LOCATION" \
  "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?alt=media"

$()執行 linux 命令並打印 output

如果您想以編程方式創建此訪問令牌,Google Auth 庫可以幫助您實現這一目標。 如果您需要代碼示例,請告訴我們您最喜歡的語言。

如果您在運行 cURL 命令的同一台機器上登錄到雲 SDK (gcloud),您可以獲得此訪問令牌。

該過程如下所示:

  1. 創建服務帳戶並授予訪問權限
  2. 生成下載json賬號秘鑰
  3. 運行命令: gcloud auth activate-service-account [ACCOUNT-NAME] --key-file=/path/to/service-key.json --project=[PROJECT_ID]
  4. 用作“授權:承載” $(gcloud auth print-access-token)

這是此過程的 Google 說明

我在我的測試項目中復制了這個,這樣的命令運行得很好:

curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -o "test.jpg" \
  "https://storage.googleapis.com/storage/v1/b/[bucket-name]/o/original.jpg?alt=media"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM