Open Telekom Cloud for Business Customers

How to call the Object Storage Service from a Java application

In the previous post we learned how to access the Object Storage Service (OBS) from the command line. In this post, we want to do this from inside a Java application. As the OBS supports the S3 protocol the AWS SDK for Java can be used for this task. Some settings need to be adjusted for this to work, though.

Creating a key

First of all, we need both an access key id and secret access key. These can be generated via the OTC console:

How to call the Object Storage Service from a Java application - creating a key


You get a csv file with exactly two lines containing the secret access key and its id:

User Name,Access Key Id,Secret Access Key
 "12345678 OTC00000000001000000123",IDABCDEFGHIJKLMNOPQR,SECRETKEYABCDEFGHIJKLMNOPQRSTUVWXYZ01234

Building the application

Now it is time to set up a project with the AWS SDK for Java. If you use maven, then all you need to do is include this dependency:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.31</version>
</dependency> 

Of course it is advisable to adjust the version number to the current stable version. If you don't use maven, simply include the libraries from the downloadable zip file from https://aws.amazon.com/sdk-for-java/ into your classpath.

Configuring the client

The AWS SDK contains a lot of AWS-specific rules regarding the structure of URLs used to communicate with S3. Therefore, we need some minor adjustments to the client for the support of "eu_de" as the region and "obs" as the service name:

public class OtcObsClient extends AmazonS3Client {
     public OtcObsClient(AWSCredentials credentials, ClientConfiguration clientConfiguration) {
         super(credentials, clientConfiguration);
         this.setSignerRegionOverride("eu-de");
         this.setRegion(new Region(new InMemoryRegionImpl("eu-de", "otc.t-systems.com")));
     }
     @Override
     protected String getServiceNameIntern() {
         return "obs";
     }
 } 

The communication protocol expects a signature with every request. This signature includes the fixed service name "s3" instead of the service name used for the construction of the URLs, which we set to "obs". So we also need a custom request signer to use this fixed service name:

public class OtcObsSigner extends AWSS3V4Signer {
     public OtcObsSigner() {
         super();
         this.serviceName = "s3";
     }
     @Override
     public void setServiceName(String serviceName) {
         // we ignore other service names
     }
 } 

Example

Now we are ready to give it a try using the credentials we generated above. Please note, that the terminology differs slightly. The accessKey is the "Access Key Id" from above. The following code shows a list of buckets:

public class OtcObsExample {
     public static void main(String[] args) {
         String accessKey = "IDABCDEFGHIJKLMNOPQR";
         String secretKey = "SECRETKEYABCDEFGHIJKLMNOPQRSTUVWXYZ01234";
         AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
         ClientConfiguration clientConfig = new ClientConfiguration();
         clientConfig.setProtocol(Protocol.HTTPS);
         clientConfig.setSignerOverride("OtcObsSigner");
         SignerFactory.registerSigner("OtcObsSigner", OtcObsSigner.class);
         AmazonS3 obs = new OtcObsClient(credentials, clientConfig);
         List<Bucket> buckets = obs.listBuckets();
         for (Bucket : buckets) {
             System.out.println(bucket.getName() + "\t" +
                     StringUtils.fromDate(bucket.getCreationDate()));
         }
     }
 } 

Depending on the buckets you have in your OBS the output will be something like this:

My first bucket    2016-07-14T15:21:09.262Z

Second bucket    2016-08-31T13:45:42.132Z

And that's it. The rest is just nice-to-have. For example, one could wrap the initialization with the signer registration inside a factory method. This was done in the github project otc-obs-java-client. There is also a complete example showing the usage of the main OBS functions creating and deleting buckets and objects.

For more background information about the OTC API in general see this post.


Daniela Ebert Daniela Ebert spent many years as an AIX Engineer in the Solution Delivery department at T-Systems. Leading up to the launch of Open Telekom Could at CeBIT 2016, she developed the technical implementation of the platform. At the moment her work is concentrated on technical development, the evaluation of technical features, and architecture issues.

Book now and claim starting credit of EUR 250* (code: 4UOTC250)
24/7 Service
Take advantage of our consulting services!

Our experts will be happy to help you.

We will answer any questions you have regarding testing, booking and usage – free and tailored to your needs. Try it out today!

Hotline: 24 hours a day, seven days a week 

0800 33 04477 from Germany
00800 44 556 600 from abroad

* Voucher can be redeemed until June 30, 2020. Please contact us when using the voucher for booking. The discount is only valid for customers with a billing address in Germany and expires two months after conclusion of the contract. The credit is deducted according to the valid list prices as per the service description. Payment of the credit in cash is excluded.


  • Test it today – with no obligation and free of charge

    Book now and claim starting credit of EUR 250*
    Code: 4UOTC250

    Book now

  • Telefon

    Free expert hotline

    Our certified cloud experts provide you with personal service free of charge.

    0800 33 04477 (from Germany)

    24 hours a day, seven days a week

  • E-Mail

    Our customer service is available free of charge via E-Mail

    Write an E-Mail

  • Arrange an appointment

    Our Open Telekom Cloud experts provide you with free, non-binding and idividual support

    Arrange an appointment