Powershell Upload, Download file Google Storage Bucket
Today in this article we will see how to automate and perform Powershell Upload, Download file Google cloud Storage Bucket
Today in this article, we will cover below aspects,
Getting Started
Prerequisites
- Create an account in the google cloud project.
- Install the Google Cloud SDK
Above google cloud SDK needs to be installed on the Server or VM where we need to run the target scripts.
Powershell – Upload the File to Google Cloud Storage Bucket
Let’s use the below image(the official logo of the TheCodebuzz) as an example. We shall be uploading this file to the GCP bucket.
Let’s save the above file to the computer in the path C:\test\thecodebuzz.png
Make sure to activate the service account as below by providing the service account file path as below,
gccloud auth activate-service-account --key-file $googleServiceAccount
Command Pattern
gsutil cp [target-file-path] gs://[gcs-path]
Example
try
{
#Source file path
$sourFilePath = "C:\test\CloudBlobTest.pdf"
#Google Service Account
$googleServiceAccount = dir "$sourFilePath\google-svc-thecodebuzz.json"
#Destination path
$destinationPath = "gs://thecodebuzz/"
#Activate the google service account
gccloud auth activate-service-account --key-file $googleServiceAccount
#Copy files to GCS bucket
gsutil cp $sourFilePath $destinationPath
}
catch
{
echo $_.Exception.GetType().FullName,$_.Exception.Message
}
After successful execution of the commands file will be uploaded to the google cloud storage bucket.
Powershell – Download the File to Google Cloud Storage Bucket
We shall now download the above-uploaded file from google Bucket using gsutil command line as below,
Command Pattern
gsutil cp gs://[gcs-file-path] [target-file-path]
Example
try
{
#Source file path
$sourFilePath = "gs://thecodebuzz/load-from-gcutil/thecodebuzz.png"
#Google Service Account
$googleServiceAccount = dir "$sourFilePath\google-svc-thecodebuzz.json"
#Destination path
$destinationPath = "C:\Test\gcp\Download"
#Activate the google service account
gccloud auth activate-service-account --key-file $googleServiceAccount
#Copy files from GCS bucket
gsutil cp $sourFilePath $destinationPath
}
catch
{
echo $_.Exception.GetType().FullName,$_.Exception.Message
}
Once the command is successful, you shall see the file generated in the path.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
References :
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.