Google Cloud Rest API Integration: 2 Easy Methods

on API, Data Integration, Google Cloud Platform, REST API, Tutorials • October 19th, 2021 • Write for Hevo

Cloud storage has become an important part of data today as it makes it easier for users and analysts to store and access their data in the Cloud at any point in time without hassle. Many corporations have come up with different Cloud offerings to provide customers with storage services and so there is a lot of competition in the market to cater to the needs of users. Google Cloud Storage (GCS) is one such platform that is popular for its flexible and scalable Cloud Storage services.

To be able to use any of this data storage, you will need to know how to access them and the parameters needed for use, REST APIs such as JSON and XML are commonly used to do this as you can connect directly with your storage unit through your REST APIs to update, retrieve, or modify information by sending requests and receiving a response to this effect.

This article will introduce Google Cloud Storage (GCS) and REST APIs and will discuss how you can use the REST APIs of JSON and XML to set up the Google Cloud REST API Integration. It will also explain useful components associated with GCS. Read along to learn more about these 2 tools!

Table of Content

Introduction to Google Cloud Storage (GCS)

Google Cloud REST API Integration: GCS Logo
Image Source

Google Cloud-based Storage (GCS) provides online storage services using Google’s infrastructure for companies. Its services are accessible worldwide and you can experience storage and retrieval of data of any amount for affordable prices. It combines the performance and scalability of Google Cloud with advanced security and sharing capabilities to serve you better.

Google Cloud Storage is also used for a range of situations that includes serving website content, archival and disaster recovery data, providing large data objects to users, etc.

To learn more about Google Cloud Service, visit here.

Introduction to REST APIs

Google Cloud REST API Integration: REST API Logo
Image Source

REST is an acronym for REpresentational State Transfer regarded as an architectural style for distributed hypermedia systems, it was created to guide the design and development of structures for the World Wide Web. 

REST defines a set of constraints of how an internet-scale distributed hypermedia system like the web should function and it lays emphasis on the scalability of the interactions between components, uniform interfaces, deployment of components, etc. REST was created by computer scientist Roy Fielding.

The REST API is an application programming interface that allows for interaction with RESTful web services by conforming to the constraints of REST architectural style and following the sets and protocols provided to build and integrate other applications. A web API that obeys the REST constraints is referred to as being RESTful and the REST API acts as the connection between an information provider and the information user. In a RESTful Web service, requests made to a resource URL will get a response in HTML, JSON, XML, or some other format.

To learn more about REST APIs, visit here.

Simplify your Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to Load Data from any data source such as Databases, SaaS applications, Cloud Storage, SDK,s, its and Streaming Services and simplifies the ETL process. It supports 100+ data sources like Google Cloud Storage and REST APIs and loads the data onto the desired Data Warehouse, enriches the data, and transforms it into an analysis-ready form without writing a single line of code.

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensure that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different Business Intelligence (BI) tools as well.

Get Started with Hevo for Free

Check out why Hevo is the Best:

  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
  • Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

Components Required for Google Cloud REST API Integration

This section is going to look at the following properties components associated with the Google Cloud REST API Integration:

Google Cloud REST API Integration Component 1: Authentication Access Token

To be able to connect REST API to GCS, you will first need an authentication access token which can be accessed from Google’s playground online here.

The access tokens usually last for 1 hour. Once it is generated, you can then proceed to get the Cloud Storage authentication.

Google Cloud REST API Integration Component 2: Buckets

The Buckets resource represents a bucket in GCS where they usually contain objects which can be accessed by their methods. They are always owned by the project team owners group. 

Buckets have the following HTTP request

PATCH https://storage.googleapis.com/storage/v1/b/bucket

For further reading on Buckets, visit here.

Google Cloud REST API Integration Component 3: Objects

This is used to update a data blob’s associated metadata and has the following HTTP request:

PATCH https://storage.googleapis.com/storage/v1/b/bucket/o/object

Google Cloud REST API Integration Component 4: Request Endpoints

These are Request Endpoints (URLs) that are used to access GCS, it has support for HTTP/1.2,  HTTP/2, and HTTP/3 protocols. 

To make an API request using JSON API, use the following URL in the example below:

https://storage.googleapis.com/download/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?alt=media

To make an API request using XML API, use the following URL in the example below:

https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME

For further reading about Request Endpoints, visit here.

Methods to Set Up the Google Cloud REST API Integration

You can connect Google Cloud Storage to REST APIs using the following 2 methods:

Method 1: Using XML API for Google Cloud REST API Integration

Google Cloud REST API Integration: XML Logo
Image Source

The XML API is an interface that lets you manage Google Cloud Storage data in a programmable fashion, it is majorly used by software developers. XML is a RESTful interface as it relies on HTTP methods and scoping information to define and execute the operations to be carried out.

The method information can be specified using standard HTTP methods such as DELETE, GET, HEAD, and PUT while the scoping information can be specified using a publicly accessible endpoint (Uniform Resource Identifier) and other scoped parameters. The primary scoping parameter for the XML API is the bucket and object names. The Cloud Storage XML API offers a web-based interface that enables you to make HTTP requests and manage responses as each of the requests executes a standard HTTP method, HTTP request headers can also be used along with the methods. 

Every request you make to GCS is always authenticated except for requests made to anonymously accessible objects or buckets. Below is a sample request retrieving a list of objects written in XML API with a bucket name my-travels and has the prefix /europe/spain.

GET /?list-type=2&prefix=/europe/spain/ HTTP/1.1
Host:my- trave.storage.googleapis.com
Date: Wed, 17 Feb 2010 15:31:56 -0800
Content-Length: 0
Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg

For further reading on XML API, visit here.

Method 2: Using JSON API for Google Cloud REST API Integration

Google Cloud REST API Integration: JSON Logo
Image Source

The JSON API is a simple interface that lets you access and manipulates your GCS projects through programming methods. Like the XML API, it is majorly used by software developers. It uses web programming and the creation of applications through web services using HTTP methods, the current version of the JSON API is v1.

Using JSON API, you can ask GCS to send only the fields you need as opposed to sending the full representation of your request in its response. This practice is known as partial response and it provides better performance of your requests. To use partial response, the Fields request parameter is used to specify the specific fields you want to be returned and it can be used along with any requests that return response data.

The format of the Fields requests parameter value is loosely based on the XPath syntax and needs you to use a comma-separated list to return multiple fields, use a/b to return a field b that is nested within field a, use a sub-selector to request a set of specific sub-fields of arrays. 

When a valid request that includes the Fields query parameter is processed by a server, it sends back an HTTP 200 OK status code along with the requested data to show it was successful; otherwise, when invalid, it returns an HTTP 400 Bad Request status code along with an error message. An example of a partial request and response is shown below:

https://storage.googleapis.com/storage/v1/b/my-bucketT/o/my-object?fields=id,name,metadata/key1

https://storage.googleapis.com/storage/v1/b/my-bucketT/o/my-object?fields=id,name,metadata/key1

200 OK

{
  "id": "my-bucket/my-object.png/456456456456",
  "name": "my-object.png",
  "metadata": {
    "key1": "val1"
  }
}

The JSON API also allows you to send updated values for specific fields to a resource metadata to modify it by using the HTTP PATCH verb known as partial updates. PATCH supports buckets, objects, defaultObjectAcessControls, etc resources.

When you use PATCH, the modified data is merged into the data for the parent object. This may include Add, Modify, and Delete. An example of a PATCH request to update the metadata of a GCS JSON API object and its response is shown below.

PATCH https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME
Authorization: Bearer OAUTH2_TOKEN
Content-Type: application/json
 
{
  "metadata": {"NEW_KEY" : "NEW_VALUE"}
}

Response

200 OK

{
  "id": "OBJECT_ID",
  "name": "OBJECT_NAME",
  "bucket": "BUCKET_NAME",
  "metadata": {
    "EXISTING_KEY" : "EXISTING_VALUE",
    "NEW_KEY" : "NEW_VALUE"
  },
  ...
}

Generally, to use JSON query parameters with a request add “?”, the name of the query parameter, and the desired value to the end of the request as seen below:

https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?QUERY_PARAMETER=VALUE

For further information about the GCS JSON API, visit here.

Conclusion

This article introduced you to Google Cloud Storage and REST APIs. It further discussed the REST APIs of JSON and XML and explained how to connect them to Google Cloud Storage (GCS). It also explained the important parameters which are needed for a successful Google Cloud Rest API connection.

Visit our Website to Explore Hevo

It can be seen that if you are not a software developer and don’t have the required skills to execute such a connection, it can be rather frustrating and this is where Hevo Data comes in. Hevo-Data will automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. This platform allows you to transfer data from 100+ multiple sources like REST APIs and Google Cloud Storage to Cloud-based Data Warehouses like Snowflake, Google BigQuery, Amazon Redshift, etc. It will provide you with a hassle-free experience and make your work life much easier.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your understanding of the Google Cloud Rest API Integration in the comments below!

No Code Data Pipeline For Your Data Warehouse