Last active
February 10, 2024 04:17
-
-
Save srijan/01edf492a7d2ac6dc36f9690bde7d626 to your computer and use it in GitHub Desktop.
Script to download a file from GCS bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 ./