The IBM® Cloud Object Storage SDK for Java how-to get a client sourcing credentials from a credentials file?

386 Views Asked by At

I'm trying to use The IBM® Cloud Object Storage SDK for Java and following this explanations https://console.bluemix.net/docs/services/cloud-object-storage/libraries/java.html#client-credentials where it says:

After generating a Service Credential, the resulting JSON document can be saved to ~/.bluemix/cos_credentials. The SDK will automatically source credentials from this file unless other credentials are explicitly set during client creation

So I settled the referenced ~/.bluemix/cos_credentials in place (got it from my IBM cos instance credentials) and i expect to use this file to configure the client instead of coding the values. So now, how can a client be instantiated? Which classes of the sdk are to be used to get a working client configured to work with the bucket?

Here my cos_credentials file

{
"apikey": "xxxxxxxxx",
"endpoints": "https://cos-service.bluemix.net/endpoints",
"iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:cloud-object-storage:global:a/xxxxxxxxxxxxx:xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx::",
"iam_apikey_name": "auto-generated-apikey-xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
"iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/xxxxxxxxxxxxxxxxxx::serviceid:ServiceId-xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:a/xxxxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx::"
}
1

There are 1 best solutions below

2
On

You should need just a few of the client classes, and none of the credential classes. Here's an example that might help get you started:

import java.sql.Timestamp;
import java.io.File;

import com.ibm.cloud.objectstorage.ClientConfiguration;
import com.ibm.cloud.objectstorage.SDKGlobalConfiguration;
import com.ibm.cloud.objectstorage.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3ClientBuilder;
import com.ibm.cloud.objectstorage.services.s3.model.ListObjectsRequest;
import com.ibm.cloud.objectstorage.services.s3.model.ObjectListing;
import com.ibm.cloud.objectstorage.services.s3.model.S3ObjectSummary;

public class CredentialsFile
{

    private static AmazonS3 _s3Client;

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        SDKGlobalConfiguration.IAM_ENDPOINT = "https://iam.bluemix.net/oidc/token";

        String bucketName = "<bucket-name.";
        String objectKey = "<object-key";
        String filePath = "/absolute/path/to/file";
        String endpoint_url = "https://s3-api.us-geo.objectstorage.softlayer.net";
        String location = "us";

        System.out.println("Current time: " + new Timestamp(System.currentTimeMillis()).toString());

        _s3Client = createClient(endpoint_url, location);

        newObject(bucketName, objectKey, filePath, _s3Client);
        listObjects(bucketName, _s3Client);
    }

    /**
     * @param bucketName
     * @param clientNum
     * @param endpoint_url
     * @param location
     * @return AmazonS3
     */
    public static AmazonS3 createClient(String endpoint_url, String location)
    {
        ClientConfiguration clientConfig = new ClientConfiguration().withRequestTimeout(5000);
        clientConfig.setUseTcpKeepAlive(true);

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new EndpointConfiguration(endpoint_url, location)).withPathStyleAccessEnabled(true)
                .withClientConfiguration(clientConfig).build();
        return s3Client;
    }

    /**
     * @param bucketName
     * @param keyName
     * @param filePath
     * @param s3Client
     */
    public static void newObject(String bucketName, String keyName, String filePath, AmazonS3 s3Client)
    {
        System.out.println("Uploading new object " + keyName + " from " + filePath + "...");
        s3Client.putObject(bucketName, keyName, new File(filePath));
        System.out.println(keyName +" uploaded successfully.");
    }

    /**
     * @param bucketName
     * @param s3Client
     */
    public static void listObjects(String bucketName, AmazonS3 s3Client)
    {
        System.out.println("Listing objects in bucket " + bucketName);
        ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest().withBucketName(bucketName));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();
    }

}