Pseudo File Server with Google Cloud Storage and GoLang.
Introduction
We know the importance of file servers in web applications. There are many use cases where we need to make the web resources(images, files, etc.) accessible to the clients.
A file server does not normally perform computational tasks or run programs on behalf of its client workstations.
File servers are commonly found in offices, where users use a local area network to connect their client computers for accessing resources.
It serves as a location for your company’s data.
In the emerging era of technology and public clouds, a similar setup can be migrated to public clouds.
Google Cloud Product (GCP) provides one such offering in the form of Google Cloud Storage(GCS).
Google Cloud Storage(GCS) is a RESTful online file storage web service for storing and accessing data on Google Cloud Platform infrastructure. The service combines the performance and scalability of Google’s cloud with advanced security and sharing capabilities. It is an Infrastructure as a Service (IaaS), comparable to Amazon S3 online storage service.
GoLang is getting popularity for its ease-of-use and highly concurrent nature.
In this post, we will build a simple web service client which will use Google Cloud Bucket as Storage using GoLang.
We will build a couple of Endpoints for storing and retrieving documents from Google bucket.
So, let's Get Started …
Setting up
We will be using GCS cloud lib for Golang.
You can download the dependency by running the below command in terminal on mac or equivalent CLI.
- Get the Google cloud library.
go get -v cloud.google.com/go/storage
2. Download Service account credentials from your Google console Account.
3. Set the below Environment variable so that the service account can be accessed by your client.
Set export GOOGLE_APPLICATION_CREDENTIALS=[PATH] as ENV variable, where [PATH] is the location of google credentials on file system.
Functionality
We are going to add the below functionality as part of the cloud integration.
- Upload feature which will take multi-form data as input and upload it to Google Storage.
- Download feature which will consume the resource link as part of the request and returns the corresponding resource by fetching it from GCS.
- Additionally, we will create a Singleton Storage client that will be used as a bridge for connecting your API and GCS.
Creating Singleton storage client which will be used for all cloud interactions.
File Upload
Upload Feature Implementation. This will take multi-form data as part of the request and store it to GCS.
File Download
Here’s the Download functionality, it will take a resource name as a path parameter. We will return a set of response headers as part of the response.
Assuming the client will be browser in most of the cases we can set an additional header named Content-Disposition that will take value in the form of <disposition-type>; filename=<file-name>
where <disposition-type>
can take a value either inline
to render the content inlined or attachment
to download it as an attachment.
Routes
We will have two routes configured for upload and download endpoint respectively.
Finally
Here’s the final main file. Try it out!!!