Normally when you order a persistence service on IBM Cloud, such as Cassandra or MySQL, the storage is already integrated into the service and developers don’t have to deal with it directly. However, you may occasionally need to do that. For example, you might have a local database for developmental convenience but wanted to sync with IBM Cloud Object Storage. In this blog, we’ll take a look at how that’s done.

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

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

Here is a code snippet 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 a fairly standard GRUD operation.

Now let’s look at how to create a 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 convenience, I 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 needs 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 *