适用于Java的IBM®Cloud Object Storage SDK如何从凭证文件获取客户端采购凭证?

时间:2018-11-14 13:56:00

标签: ibm-cloud object-storage

我正尝试使用IBM®Cloud Object Storage SDK for Java,并按照以下说明https://console.bluemix.net/docs/services/cloud-object-storage/libraries/java.html#client-credentials进行说明:

  

生成服务凭证后,可以将生成的JSON文档保存到〜/ .bluemix / cos_credentials。除非在客户端创建过程中明确设置了其他凭据,否则SDK会自动从该文件中获取凭据

因此,我将引用的〜/ .bluemix / cos_credentials解决到位(从我的IBM cos实例凭证中获取了它),并且我希望使用此文件来配置客户机,而不是对值进行编码。那么,现在如何实例化客户端?哪些类别的sdk将用于配置工作桶以与存储桶一起工作?

这是我的cos_credentials文件

{
"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 个答案:

答案 0 :(得分:0)

您应该只需要几个客户端类,而不需要任何凭证类。这是一个可能帮助您入门的示例:

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();
    }

}