Sample Go projects to access GCP via SDK.
Nothing.
This project creates a REST API which receives a JSON of the form {"content": <string>}
and uploads its content
as <timestamp>.txt
to Cloud Storage. A signed URL (i.e. a public URL with expiration date) to access the object uploaded in Cloud Storage is returned.
-
Access IAM & Admin console.
-
Select
IAM
in the sidebar. -
Click the edit icon for the principal whose value of
Name
field isCompute Engine default service account
. -
Select
ADD ANOTHER ROLE
to addService Account Token Creator
role.
-
-
Access Cloud Storage console to created a bucket named
test-bucket-001-a
. -
Access Cloud Functions console to create a function with the following settings.
-
Function name:
function-1
-
Environment:
2nd gen
-
Region:
us-central1
-
Runtime:
Go 1.20
-
-
Upload this project to deploy a function.
$ gcloud functions deploy function-1 --source . --gen2 --region us-central1
-
Privately test the API. (At this time, the API is not public so we need
Authorization
header.)$ curl <URL> \ -H "Authorization: bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"content": "hello"}'
{ "status": "success", "url": "https://..." }
$ curl <returned URL>
-
Access API Gateway console and select
CREATE GATEWAY
to create a gateway. For API Spec, upload./cloud_functions/openapi.yaml
. -
Now the API is public via API Gateway URL. Call the API.
$ curl <URL> \ -H "Content-Type: application/json" \ -d '{"content": "hello"}'
{ "status": "success", "url": "https://..." }
$ curl <returned URL>
This project deploys a very simple Docker container to Cloud Run.
-
Interactively build and deploy
Dockerfile
. Allow unauthenticated invocations to issue a public URL.$ gcloud run deploy
Allow unauthenticated invocations to [cloudrun] (y/N)? y
-
Check the public URL of the deployment.
$ gcloud run services list
-
Call the API.
$ curl <URL>
Working.
This project creates a REST API which receives a JSON of the form {"content": <string>}
and uploads its content
as <timestamp>.txt
to Cloud Storage. A signed URL (i.e. a public URL with expiration date) to access the object uploaded in Cloud Storage is returned. In addition, every request is logged to SQL (PostgresSQL) and Firestore.
-
Access Firestore console.
-
If this is the very first time you visit the console, you may be requested to select
Native
mode orDatastore
mode. SelectNative
. -
You don't have to manually create any collection as it will be automatically created when accessed via SDK.
-
-
Access SQL console.
-
Select
CREATE INSTANCE
. -
Choose
PostgresSQL
. -
Create an instance named
test-sql-001
. -
Select
Connections
in the sidebar. -
Select
ADD NETWORK
to add a network and specify your IP address plus assignPrivate IP
.
-
-
Create a database in the SQL instance.
-
Connect.
$ PGPASSWORD=<password> psql -h <URL> -p 5432 -U postgres
-
Create a database named
test
.CREATE DATABASE test;
-
Check the result by listing all the databases.
\list
-
Optionally change the password.
ALTER USER postgres PASSWORD '<new password>';
-
-
Access Compute Engine console.
-
Select
CREATE INSTANCE
to create an instance namedinstance-2
. SelectAllow full access to all Cloud APIs
forAccess scopes
. (To edit the value ofAccess scopes
after creating the instance, first it shall be stopped.) -
Create a SSH key.
$ gcloud compute ssh instance-2
-
Connect to the instance via SSH.
$ ssh gcp
-
Install some packages.
$ sudo apt update $ sudo apt install rsync screen
-
Install Go by following the official instructions.
$ curl -L -O 'https://go.dev/dl/go1.20.2.linux-amd64.tar.gz' $ sudo tar -C /usr/local -xzf go1.20.2.linux-amd64.tar.gz $ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
-
-
Access VPC network console.
-
Select
Firewall
in the sidebar. -
Select
CREATE FIREWALL RULE
to create a rule which opens8080
port.
-
-
Send this project to the instance.
$ rsync -auv ./compute_engine gcp:./
-
Run the server.
-
Connect to the instance.
$ ssh gcp
-
Build.
$ cd compute_engine/ $ go build main.go
-
Write a config file (see below for the details).
$ vi config.json
-
Run the server.
$ screen -d -m ./main $ screen -ls
-
-
Call the API.
$ curl <URL>:8080 -d '{"content": "hello"}'
{ "status": "success", "url": "https://..." }
$ curl <returned URL>
Configurations are read from config.json
.
Example:
{
"port": 8080,
"cloud_storage": {
"bucket_name": "test-bucket-001-a"
},
"firestore": {
"project_id": "my-project-xyz",
"collection_name": "test_collection_001"
},
"postgres": {
"user": "postgres",
"password": "password",
"host": "10.xxx.xx.xxx",
"port": 5432,
"database_name": "test",
"table_name": "test_table"
}
}