Introduction
This article shows how to serve files with Google Cloud Run. There are a few reasons to use Cloud Run instead of Google Cloud Storage.
- To learn how to deploy a simple file sharing service using Cloud Run.
- To have a method of measuring performance. You can have a variety of file sizes in your image to test downloads with.
- You don’t want to bother with the issues of public file permissions with Cloud Storage.
- Using Cloud Run is so simple to set up. You can share your vacation photos with family and friends in 30 minutes.
- Setup a custom domain name if you own your own domain.
Download Git Repository
I have published the files for this article on GitHub.
License: MIT License
Clone my repository to your system:
1 |
git clone https://github.com/jhanley-com/google-cloud-run-simple-file-server-in-go.git |
Getting Started
Verify that the correct project is the default project:
1 |
gcloud config list core/project |
If the correct project is not displayed, use this command to change the default project:
1 |
gcloud config set core/project [MY_PROJECT_ID] |
You can list the projects in your account. Some security configurations will not allow you to list projects. In that case, you will need to specify the default project manually as shown above.
1 |
gcloud projects list |
Setup files for sharing
Inside the repository is a directory named “data”. This directory is the home directory for file sharing. Copy files to this directory. Subdirectories are supported, so you can have a tree of files.
Review the program source code
The key Go package is net/http
. This library provides the entire File Server interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package main import ( "log" "os" "net/http" ) func main() { directory := "./data" port := os.Getenv("PORT") if port == "" { port = "8080" } http.Handle("/", http.FileServer(http.Dir(directory))) log.Printf("Serving %s on HTTP port %s\n", directory, port) log.Fatal(http.ListenAndServe(":" + port, nil)) } |
Setup the build scripts
Change directories to scripts-windows
. Edit the file env.bat
. Change any of the environment variables. The important one is GCP_NAME
as this will be the name for the Cloud Run service. Provided that the Cloud SDK is set up correctly with credentials and default Project ID, the script will build everything automatically.
To build the image and deploy to Cloud Run, execute the command: build.bat
.
Credits
I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels.
I design software for enterprise-class systems and data centers. My background is 30+ years in storage (SCSI, FC, iSCSI, disk arrays, imaging) virtualization. 20+ years in identity, security, and forensics.
For the past 14+ years, I have been working in the cloud (AWS, Azure, Google, Alibaba, IBM, Oracle) designing hybrid and multi-cloud software solutions. I am an MVP/GDE with several.
Leave a Reply