以下是我用于listSummaries
个对象的代码(它与http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html的99%相同)。
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
System.out.println("bucketname = " + bucketName + "key =" + key);
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(key);
List<S3ObjectSummary> summaries = new ArrayList<S3ObjectSummary>();
ObjectListing objectListing;
do {
line 147 >>> objectListing = s3client.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
summaries.add(objectSummary);
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
这里说它可以使用匿名凭据来访问可公开访问的存储桶(http://docs.aws.amazon.com/AmazonS3/latest/dev/AuthUsingAcctOrUserCredJava.html)我确信我访问的存储桶是开源的。我收到以下错误消息,我不知道为什么会发生这种情况:
Exception in thread "main" java.lang.IllegalArgumentException: AWS credential profiles file not found in the given path: /home/xxx/.aws/credentials
at com.amazonaws.auth.profile.internal.ProfilesConfigFileLoader.loadProfiles(ProfilesConfigFileLoader.java:45)
at com.amazonaws.auth.profile.ProfilesConfigFile.loadProfiles(ProfilesConfigFile.java:194)
at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:119)
at com.amazonaws.auth.profile.ProfilesConfigFile.<init>(ProfilesConfigFile.java:93)
at com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:149)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3589)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3548)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:647)
at xxx.xxx.xxx.LocalInterface.ListBucket(LocalInterface.java:147)
答案 0 :(得分:3)
我想出了自己的答案。我没有使用匿名凭据。以下是如何使用它们:
AWSCredentials creds = new AnonymousAWSCredentials();
AmazonS3 s3client = new AmazonS3Client(creds);
希望这有助于某人。