Emulate Google Cloud Storage using fake-gcs-server

When working with Cloud Providers, we need to consider solutions to save costs, at least at the development stage. Because most of the services of the Cloud Providers that we need to use will cost more or less money. One solution that I introduce to you in this article is about emulating Google Cloud Storage using an open-source named fake-gcs-server, suitable for projects that use Google Cloud Platform. It helps us to start up a standalone server with similar functionality to Google Cloud Storage. You can use Google’s libraries to work with Google Cloud Storage, to work with this open-source.

Install fake-gcs-server

fake-gcs-server provides us with a Docker Image, so we can easily start it up with just a few steps.

Specifically, you need to run the following Docker command:

Now, if you request to the URL to get information Buckets list with host port using fake server: https://localhost:4443/storage/v1/b, you will see the following results:

Emulate Google Cloud Storage using fake-gcs-server

Items shows empty because we don’t have a bucket in fake-gcs-server yet.

You can mount some available data and run fake-gcs-server as follows:

${PWD} is the current directory where you are running the Docker command plus with the example/data directory, which will be mapped to the /data directory inside the fake-gcs-server container. You can change ${PWD}/examples/data to whatever directory you want. The directories in this ${PWD}/examples/data directory will be buckets, and of course, the files in the buckets will be the objects of these buckets.

For example, I have a file test.txt located in the directory /Users/khanh/Documents/data/sample-bucket, I run the command start fake-gcs-server as follows:

now sample-bucket will be a bucket in fake-gcs-server, and file test.txt will be an object in this bucket.

Request the URL again https://localhost:4443/storage/v1/b, you will see the following results:

Emulate Google Cloud Storage using fake-gcs-server

Get information about all objects of the bucket with the URL https://localhost:4443/storage/v1/b/sample-bucket/o, you will see the following results:

Emulate Google Cloud Storage using fake-gcs-server

You can add as many buckets as you want, as many objects as you want.

By default, fake-gcs-server uses HTTPS, you can use HTTP by adding to the command start command the scheme parameter as follows:

Now we can use fake-gcs-server with HTTP:

Emulate Google Cloud Storage using fake-gcs-server



Use Spring Cloud GCP Storage to access the bucket of fake-gcs-server

To access buckets on Google Cloud Storage, you can use the spring-cloud-gcp-storage library. We can also use this library with fake-gcs-server.

One thing you should note is that we do not need to use credentials to access the buckets of fake-gcs-server.

I will create a simple Maven project, declare to use spring-cloud-gcp-storage to access the fake-gcs-server that I started above, as follows:

Emulate Google Cloud Storage using fake-gcs-server

with:

I will create a new main class, using the spring-cloud-gcp-storage API to access the sample-bucket bucket as follows:

Here, I do not use credentials to access the bucket of fake-gcs-server. You can operate on the bucket of fake-gcs-server similar to Google Cloud Storage using this library.

The output when running this example is as follows:

Emulate Google Cloud Storage using fake-gcs-server

Add Comment