这是我用于 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)
最佳答案
我想出了我自己的答案。我没有使用匿名凭据。以下是它们的使用方法:
AWSCredentials creds = new AnonymousAWSCredentials();
AmazonS3 s3client = new AmazonS3Client(creds);
希望这可以帮助某人。
关于java - 尝试使用匿名凭证从 AWS S3 列出对象,但它仍在检查 ~/.aws/credentials,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33966370/