package bluemixtest;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.ibm.oauth.BasicIBMOAuthCredentials;
import java.util.List;
public class BlueMixTest {
private static AmazonS3 _s3Client;
public static void main(String[] args) {
SDKGlobalConfiguration.IAM_ENDPOINT = "https://iam.bluemix.net/oidc/token";
String bucketName = "********";
String api_key = "********";
String service_instance_id = "********";
String endpoint_url = "https://s3-api.us-geo.objectstorage.softlayer.net";
String location = "us";
_s3Client = createClient(api_key, service_instance_id, endpoint_url, location);
listObjects(bucketName, _s3Client);
listBuckets(_s3Client);
}
public static AmazonS3 createClient(String api_key, String service_instance_id, String endpoint_url, String location) {
AWSCredentials credentials;
if (endpoint_url.contains("objectstorage.softlayer.net")) {
credentials = new BasicIBMOAuthCredentials(api_key, service_instance_id);
} else {
String access_key = api_key;
String secret_key = service_instance_id;
credentials = new BasicAWSCredentials(access_key, secret_key);
}
ClientConfiguration clientConfig = new ClientConfiguration().withRequestTimeout(5000);
clientConfig.setUseTcpKeepAlive(true);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
.withEndpointConfiguration(new EndpointConfiguration(endpoint_url, location)).withPathStyleAccessEnabled(true)
.withClientConfiguration(clientConfig).build();
return 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();
}
public static void listBuckets(AmazonS3 s3Client) {
System.out.println("Listing buckets");
final List<Bucket> bucketList = _s3Client.listBuckets();
for (final Bucket bucket : bucketList) {
System.out.println(bucket.getName());
}
System.out.println();
}
}
ClientConfiguration clientConfig = new ClientConfiguration()
抛出:IllegalArgumentException
堆栈跟踪:
java.lang.IllegalArgumentException at com.amazonaws.internal.config.InternalConfig.loadfrom(InternalConfig.java:235) 在 com.amazonaws.internal.config.InternalConfig.load(InternalConfig.java:249) 在 com.amazonaws.internal.config.InternalConfig $厂(InternalConfig.java:304) 在 com.amazonaws.util.VersionInfoUtils.userAgent(VersionInfoUtils.java:142) 在 com.amazonaws.util.VersionInfoUtils.initializeUserAgent(VersionInfoUtils.java:137) 在 com.amazonaws.util.VersionInfoUtils.getUserAgent(VersionInfoUtils.java:100) 在 com.amazonaws.ClientConfiguration。(ClientConfiguration.java:65) 线程“main”中的异常java.lang.ExceptionInInitializerError at com.amazonaws.util.VersionInfoUtils.userAgent(VersionInfoUtils.java:142) 在 com.amazonaws.util.VersionInfoUtils.initializeUserAgent(VersionInfoUtils.java:137) 在 com.amazonaws.util.VersionInfoUtils.getUserAgent(VersionInfoUtils.java:100) 在 com.amazonaws.ClientConfiguration。(ClientConfiguration.java:65) 在bluemixtest.BlueMixTest.createClient(BlueMixTest.java:58)at bluemixtest.BlueMixTest.main(BlueMixTest.java:44)引起: java.lang.IllegalArgumentException at com.amazonaws.internal.config.InternalConfig.loadfrom(InternalConfig.java:235) 在 com.amazonaws.internal.config.InternalConfig.load(InternalConfig.java:249) 在 com.amazonaws.internal.config.InternalConfig $厂(InternalConfig.java:304)
ibm-cos-java-sdk-core-1.1.0和ibm-cos-java-sdk-s3-1.1.0
我想解决这个问题。 来自现场的资源:ibm-cos-java-sdk
我想使用ibm-cos-java-sdk访问IBM Cloud Object Storage。
答案 0 :(得分:0)
看起来您没有使用正确的代码依赖项。
如果您使用的是maven,请将以下依赖项放在pom.xml
中<dependencies>
<dependency>
<groupId>com.ibm.cos</groupId>
<artifactId>ibm-cos-java-sdk</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>