Normally when we order a persistence service on IBM Cloud, such as Cassandra or MySQl, the storage is already integrated in the service and developers don’t have to deal with it directly. However occasionally we do need to do that. One example would be if you have a local database for developmental convenience but would like to sync with IBM Cloud Object Storage. In this blog we’ll examine how that’s done.

IBM Cloud Object Storage offers COS api, which is an implementation of S3 API, for applications to interact with it. So for your application you can use any S3 compatible libraries to access the IBM Cloud Object Storage.

Lagom is an open source opinionated reactive programming framework for building and deploying microservices. It is based on Akka toolkit and play framework. For S3 object storage access, it uses akka.stream.alpakka.s3 library.

Here is a code snipplet of the Lagom client. ... import akka.stream.alpakka.s3.MemoryBufferType; import akka.stream.alpakka.s3.Proxy; import akka.stream.alpakka.s3.S3Settings; import com.amazonaws.auth.*; import akka.stream.alpakka.s3.javadsl.MultipartUploadResult; import akka.stream.alpakka.s3.javadsl.S3Client; ... AWSStaticCredentialsProvider provider = new AWSStaticCredentialsProvider( new BasicAWSCredentials( config.getString("credentials.access-key-id"), config.getString("credentials.secret-access-key") )); ... client = new S3Client(settings, actorSystem, materializer); ...

And the rest is pretty standard GRUD operation.

Now let’s look at how to create an service instance of IBM Cloud Object Storage. There are two types of Object Storage you can create: The infrastructure and the service. They both provide S3 compatible API access. The difference between the two is similar to ordering a bare metal server vs. a vm. For the convenience I’ll prefer the service.

Let’s go to the GUI, where you can create a service and then a bucket. These are pretty straightforward. The next step will be to create a `Service credentials`. That’s where it becomes tricky. Since the COS api has it’s own preference of credential format, the generated credential is not compatible with the S3 API, which is required by the Lagom client. The trick is to put {"HMAC":true} in the optional parameter as shown in the following picture.

screenshot

In your generated credential, you’ll find the S3 style authentication information: "cos_hmac_keys": { "access_key_id": "f6dadab28f5f4b488eadfe884fd77232", "secret_access_key": "30a8b4fc70d217a863f5511347bae0d861c327f4c6cdaa3e" },

And that’s what the Lagom client needed to access your bucket.

For the complete example, check out the code pattern. Happy coding!

Join The Discussion

Your email address will not be published. Required fields are marked *