繁体   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