Skip to content

Instantly share code, notes, and snippets.

@srijan
Last active February 10, 2024 04:17
Show Gist options
  • Save srijan/01edf492a7d2ac6dc36f9690bde7d626 to your computer and use it in GitHub Desktop.
Save srijan/01edf492a7d2ac6dc36f9690bde7d626 to your computer and use it in GitHub Desktop.
Script to download a file from GCS bucket
#!/bin/bash
# Download artifact from GCS bucket
set -e
echo -e "====> Run \`gcloud auth print-access-token\` on a system where you've setup gcloud to get access token\n"
read -r -p "Enter access token: " StorageAccessToken
read -r -p "Enter GCS artifact URL: " ArtifactURL
if [[ "${ArtifactURL:0:33}" == "https://console.cloud.google.com/" ]]; then
BucketAndFile="${ArtifactURL#*https://console.cloud.google.com/storage/browser/_details/}"
elif [[ "${ArtifactURL:0:33}" == "https://storage.cloud.google.com/" ]]; then
BucketAndFile="${ArtifactURL#*https://storage.cloud.google.com/}"
elif [[ "${ArtifactURL:0:5}" == "gs://" ]]; then
BucketAndFile="${ArtifactURL#*gs://}"
else
echo "Invalid GCS artifact URL"
exit 1
fi
StorageBucket="${BucketAndFile%%/*}"
StorageFile="${BucketAndFile#*/}"
StorageFileEscaped=$(echo "${StorageFile}" | sed 's/\//%2F/g')
OutputFileName="${StorageFile##*/}"
echo -e "\n====> Downloading gs://${StorageBucket}/${StorageFile} to ${OutputFileName}\n"
wget -O "${OutputFileName}" --header="Authorization: Bearer ${StorageAccessToken}" \
"https://storage.googleapis.com/storage/v1/b/${StorageBucket}/o/${StorageFileEscaped}?alt=media"
@9mgupta2
Copy link

Can we have StorageAccessToken=$(gcloud auth print-access-token)

@srijan
Copy link
Author

srijan commented Sep 19, 2022

@9mgupta2 The whole point of this is to download when gcloud is not installed and/or auth is not configured on the target system.

If gcloud is installed and authenticated, you can just do gcloud storage cp gs://$StorageBucket/$StorageFile ./

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment