This article is the 9th day article of WebCrew Advent Calendar 2017. Yesterday was @ kadota's "How to implement WEB push notification".
I needed to link with Google Cloud Storage (GCS) in a Java application (Java 6) running on CentOS 5 running as an application server. I realized it in a slightly acrobatic way, so I wrote this article.
A common way to integrate with Java applications is to use Google Cloud Client Library for Java to integrate with GCS. When I actually implemented a function that requires GCS linkage in another application, I tried to link with GCS using Google Cloud Client Library for Java.
However, the Google Cloud Client Library for Java requires a Java 7 or higher execution environment. This time, the Java version of the linked application was Java 6, so another method was required.
** Install CLOUD SDK on CentOS5 and execute CLOUD SDK command by external call of Java **
The procedure for installing the CLOUD SDK for Cent OS 5 is roughly divided into the following.
I will explain each of them based on the points to be noted.
The CLOUD SDK requires Python 2.7 to be installed. The default Python version for Cent OS 5 is 2.4. So I'll start by upgrading the Python version.
** You should think that you can't update Python using yum on Cent OS 5 ** ** You should think that you can't update Python using yum on Cent OS 5 ** ** You should think that you can't update Python using yum on Cent OS 5 **
I said it three times because it's important.
If you search google teacher for "centos python update", the method using yum may be hit, but it is better to think that Python update using the yum repository is not possible on Cent OS 5.
The official yum repository for Cent OS 5 is EOL and
Even if you replace the repository with a mirror site, you can use yum install such as centos-release-scl-rh (Software Collections for CentOS)
.
Let's aim for the update by steadily decompressing from the compressed file.
Download and decompress the compressed file
wget --no-check-certificate https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar xvzf Python-2.7.9.tgz
Python installation
cd Python-2.7.9
./configure --prefix=/usr/local
make
make altinstall
Python replacement
mv /usr/bin/python /usr/bin/python_bk
Download and decompress the compressed file
cd /usr/local
The location of the CLOUD SDK is as above.
wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-158.0.0-linux-x86_64.tar.gz?hl=ja tar xfvz google-cloud-sdk-158.0.0-linux-x86_64.tar.gz?hl=ja
Install CLOUD SDK
./google-cloud-sdk/install.sh
Confirm CLOUD SDK installation
/usr/local/google-cloud-sdk/bin/gcloud --help
After installing the CLOUD SDK on your application server, run the SDK commands externally in your Java application.
This section describes the procedure for uploading a file from the application server to GCS.
Granting approval using a service account See Cloud SDK Tool Authorization for more information.
```java
ProcessBuilder pb = new ProcessBuilder({"/usr/local/google-cloud-sdk/bin/gcloud", "auth", "activate-service-account", "--key-file", "[Service account key file]"});
process =pb.start();
processRes = process.waitFor();
//Error handling is performed depending on the result of processRes
```
Upload from application server to GCS with gsutil
ProcessBuilder pb = new ProcessBuilder({"/usr/local/google-cloud-sdk/bin/gsutil","mv","[Upload file path]","[Upload destination GCS bucket URL]"});
process =pb.start();
processRes = process.waitFor();
//Error handling is performed depending on the result of processRes
Starting with a slight panic, "Google Cloud Client Library for Java is not available, GCS integration !? What should I do !!", "CentOS 5, default Python is 2.4 !? What should I do !!", "yum can't be used!" There were many cases such as "What should I do !!" and "I don't know the success judgment of the external call of the SDK command !? What should I do !!" I was able to implement it firmly. Obviously, it's important not to stop thinking because you can't do it, but to try an approach like that. This case may not be very common, but I hope you get used to helping someone who is having trouble with a similar phenomenon.
Tomorrow is @ t-itou. Thank you.
WebCrew is always looking for people to work with. Please feel free to enter.
Recruitment of development engineers Recruitment of front-end engineers Recruitment of database engineers
Recommended Posts