Accessing Google API over CEN
By Vikram Godse, Solution Architect
Alibaba Cloud Cloud Enterprise Network (CEN) provides connectivity to Alibaba Cloud Virtual Private Clouds (VPCs) across the globe. This provides low latency bandwidth in addition to cross border connectivity to workloads from any international Alibaba Cloud Region into Alibaba Cloud in China
For users who have their apps built around the Google API’s there is a bit of configuration required to get it working from China. This article shows how to configure a Reverse Proxy to Access Google API’s from China over Alibaba Cloud CEN.
This approach is only to showcase accessing the custom API’s developed by an organization and hosted on Google API Gateway. In the long term, it is recommended that the custom API’s be moved to Alibaba Cloud API Gateway.
- Alibaba Cloud Account
- 2 VPC’s one in China Region and the other in the International region. We have considered Beijing and London in this document
- 2 ECS instances 1 in Beijing VPC and the other in the London VPC
- CEN Configuration Across the two VPC’s
- Access to Google APIs for testing. We are using the Google Texttospeech API and the Google Translate API for the test.
The diagram above depicts the overall connectivity between the Alibaba Cloud regions between Beijing and London and the connectivity to Google API over the internet
- The VPC in Beijing hosts an ECS instance running Centos 7 which will be the client machine wanting to access the Google APIs
- The VPC in London hosts an ECS instance running Centos 7 and Apache 2.4.6
- The VPC in London connects to Google API’s over standard Internet
- The IP for the ECS instance in Beijing is 192.168.0.201
- The IP for the ECS instance in London is 172.16.0.31
Setup two hostnames in the /etc/hosts file on the Beijing server pointing to the London server for eg.
These hostnames will be the Virtual hosts that we will configure on the London Webserver, one each for the Google Translate and Text-to-Speech API’s
Install and Configure Apache
Install Apache on the Centos Server in London
The modules that are needed to use Apache as a reverse proxy include mod_proxy itself and several of its add-on modules, which extend its functionality to support different network protocols. Specifically, we will be using:
mod_proxy, the main proxy module Apache module for redirecting connections; it allows Apache to act as a gateway to the underlying application servers.
mod_proxy_http, which adds support for proxying HTTP connections.
mod_lbmethod_byrequests, which add load balancing features for multiple backend servers.
All these modules are enabled by default, but we can check the modules installed
If any of the modules are not listed you can enable them by editing the /etc/httpd/conf.modules.d/00-proxy.conf
And un-commenting the required modules by removing the #
Save the file and then restart the httpd service
For SSL to be supported you need to install mod_ssl
Configure Apache for Reverse Proxy
Setup two virtual hosts one for the “Google Translate API” and another for the “Text-to-Speech” API
Create an Apache Config file in the /etc/httpd/conf.d/google-translate.conf and make the following entries
The request for google translate will be received by this virtual host “translate.rentokillondon.com” over the CEN, which is in London and it will proxy the request to Google Translate over https using the Google API URL https://translate.googleapis.com It will forward the URL context as it has received from the request.
An example request to translate some content from English to German is shown in the following diagram
We can create another Virtual host for Google-Text-to-Speech API
Create another virtual host by creating another configuration file /etc/httpd/conf.d/textospeech.conf and make the following entries
Restart the httpd server after making the virtual host entry.
Now from the server in Beijing when we have to call the Text-to-Speech API we will call http://texttospeech.rentokillondon.com/ which is our reverse proxy virtual host in London.
This reverse proxy will proxy the request to https://texttospeech.googleapis.com/
It will include the URL context that is received in the host header and forward it to google.
Following is a sample call that will convert some text in French to a voice file in base64 text format which can be decoded to generate the MP3 voice file
The voice file in base64 format looks as follows
Using base64 we can decode the file to a binary mp3 format audio file
The output file is as the audio.mp3 file
In this article, we showed the configuration for accessing Google API’s from Alibaba Cloud in China using Cloud Enterprise Network (CEN). However this exercise has been created for users who are using googleapi to host their custom API’s outside China and want to use the same API’s in China. You are recommended to move the API’s to Alibaba Cloud API Gateway in the long term.