CodeAlchemy

Jotting one man's journey through software development, programming, and technology


Project maintained by pablogarciaprado Hosted on GitHub Pages — Theme by mattgraham

◀️ Home

Google Cloud Commands

Google Cloud Storage

Copy a file to or from a Google Cloud Storage bucket.

gcloud storage cp copies files between local storage and Google Cloud Storage (GCS) or between GCS buckets. The basic syntax is:

gcloud storage cp [OPTIONS] source destination
Examples
  1. Upload a file to GCS:
    gcloud storage cp file.txt gs://my-bucket/
    
  2. Download a file from GCS:
    gcloud storage cp gs://my-bucket/file.txt .
    
  3. Copy a file between GCS buckets:
    gcloud storage cp gs://source-bucket/file.txt gs://destination-bucket/
    
  4. Detailed breakdown:
    gcloud storage cp ~/code/images/${IMAGE_NAME} gs://${UPLOAD_BUCKET}
    

    gs:// is a URI scheme used for Google Cloud Storage (GCS). It indicates that the following path refers to a bucket or object stored in GCS.

Common Options:

Deploy on App Engine

Run gcloud app deploy on the repository directory. Make sure to have a YAML configuration file within the folder.

For example, you could navigate the appropriate folder and pull the latest changes:

cd github_repos/<your_repo>
git switch main
git fetch origin
git pull

Then, deploy:

gcloud app deploy

A YAML file example:

runtime: python39

entrypoint: gunicorn -b :$PORT app:app

handlers:
- url: /static
  static_dir: static
  http_headers:
    Cache-Control: "no-cache, no-store, must-revalidate"
    Pragma: "no-cache"
    Expires: "0"

- url: /.*
  script: auto

Tail logs from a Google App Engine service

This script helps to quickly stream logs from the latest deployed App Engine version in the default service of the your-project-name project. Very useful for debugging and monitoring new deployments.

  1. Set the active GCP project:
    gcloud config set project your-project-name
    

    Sets the current project in your gcloud configuration to your-project-name, so subsequent commands are run against this project.

  2. Get the latest version ID:
    LATEST_VERSION=$(gcloud app versions list \
      --service default \
      --sort-by "~version.id" \
      --limit 1 \
      --format "value(version.id)")
    

This block:

  1. Print the latest version ID:
    echo "Latest version: $LATEST_VERSION"
    

    This line prints out which version was found, for clarity or debugging.

  2. Tail logs from the latest version:
    gcloud app logs tail \
      --service default \
      --version $LATEST_VERSION
    

    Tails (continuously streams) logs for the most recent version of the default App Engine service.

To stop the gcloud app logs tail command from tailing logs, simply press:

Ctrl + C