'gcloud' and 'gsutil' command memo

Posted: May 09, 2020

gcloud and gsutil command memo;


gcloud

Accounts

# see list of configured GCP accounts
gcloud config configurations list

# use <name>'s account credential
gcloud config configurations activate <name>

# see the current account's values
gcloud config list

# GCR login
gcloud auth configure-docker

gsutil (for Cloud Storage)

# list bucket
gsutil ls

# list dirs/objects
gsutil ls gs://my-bucket
gsutil ls -l gs://my-bucket/

# create new bucket
gsutil mb gs://my-bucket

# delete bucket
gsutil rb gs://my-bucket


# [file copy]
# gsutil <command> <src> <dst>
# Options:
#   -m = multithread download/upload
#   <command> -r = recursive

# rsync data
gsutil -m rsync -r gs://my-bucket/dir/ ./local_dir/

# copy data
gsutil -m cp -r gs://my-bucket/dir/ ./local_dir/


# generate temporary link
# gsutil signurl -d <duration> /path/to/<key file>.json gs://<bucket>/dir/object
#   - Use a key file of service account which has `Storage Object Viewer` permission.
gsutil signurl -d 24h $HOME/.keys/gcp_service_account_key.json gs://my-bucket/dir/foo.txt

> gs://my-bucket/dir/foo.txt	GET	2020-07-18 18:37:12
> https://storage.googleapis.com/my-bucket/dir/foo.txt?x-goog-signature=b737aab4d62fe4aef27593d0aa405f54c8dabeb67a23ceac332f6ea11bf13fef344f22a3e2aa927bc4417a9f0c73035f9348e3541210387b36d6c93d2fa16498273ba6dc5224abd85a6f6741be475b3d18f17e20cf651622f43ce83ebfdc1c14c6d0c6c4d21d9bec32eeee4cf25122a5bfd97f7c62bd97aa9e39f9cdd34274e168daddefa1b479aac7d1a471632d3128eae94ab82f71e9e2aca92f84d5b10fac399a14534108ed86f1c61f93a8e835fcc691fa36111109e0a31e48783c3e7b20102939a7c19a60e3331329c4f1a5383af9fa54e987d0ca86cce2c5805d467aba6fb46f2f5ac1dd14e6b72520e4a14cbae27e18aa5cd980427435b764b07d1a83&x-goog-algorithm=GOOG4-RSA-SHA256&x-goog-credential=gcs-signurl%40my-bucket.iam.gserviceaccount.com%2F20200716%2Fasia-northeast1%2Fstorage%2Fgoog4_request&x-goog-date=20200716T093712Z&x-goog-expires=172800&x-goog-signedheaders=host