[英]Service: Amazon S3; Status Code: 404; Error Code: NoSuchBucket; Trouble getting a file from IBM Cloud Object Storage
I am trying to get a file from a bucket in IBM cloud object storage.我正在尝试从 IBM 云对象存储中的存储桶中获取文件。 For this, first I am trying to read all the available files in a bucket.
为此,首先我尝试读取存储桶中的所有可用文件。
private static List<String> listBuckets(AmazonS3 cosClient) {
final List<Bucket> bucketList = cosClient.listBuckets();
List<String> bucketNames = new ArrayList<String>();
for (final Bucket bucket : bucketList) {
bucketNames.add(bucket.getName());
}
return bucketNames;
}
public InputStream getCOSFile(AmazonS3 cosClient, String bucketName, String objectName){
List<String> bucketNames = listBuckets(cosClient);
if (bucketNames.contains(bucketName)){
LOGGER.info(bucketName+" exists");
getBucketContentsV2(cosClient, bucketName, 2);
}
Here, I get the message bucketName exists from within the if block.在这里,我从 if 块中获取消息 bucketName 存在。 Also, the bucket does exist in my cloud account.
此外,存储桶确实存在于我的云帐户中。 But, getBucketContentsV2 gives me this error message: "The specified bucket does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchBucket; Request ID: xxxxx)
但是,getBucketContentsV2 给了我这个错误消息:“指定的存储桶不存在。(服务:Amazon S3;状态代码:404;错误代码:NoSuchBucket;请求 ID:xxxxx)
Here's the method getBucketContentsV2, almost exactly as in the IBM cloud doc tutorials.这是 getBucketContentsV2 方法,几乎与 IBM 云文档教程中的一样。
public static void getBucketContentsV2(AmazonS3 cosClient, String bucketName, int maxKeys) {
System.out.printf("Retrieving bucket contents (V2) from: %s\n", bucketName);
boolean moreResults = true;
String nextToken = "";
while (moreResults) {
ListObjectsV2Request request = new ListObjectsV2Request()
.withBucketName(bucketName)
.withMaxKeys(maxKeys)
.withContinuationToken(nextToken);
ListObjectsV2Result result = cosClient.listObjectsV2(request);
for(S3ObjectSummary objectSummary : result.getObjectSummaries()) {
System.out.printf("Item: %s (%s bytes)\n", objectSummary.getKey(), objectSummary.getSize());
}
if (result.isTruncated()) {
nextToken = result.getNextContinuationToken();
System.out.println("...More results in next batch!\n");
}
else {
nextToken = "";
moreResults = false;
}
}
System.out.println("...No more results!");
}
I also tried retrieving all files in a bucket with V1 code in the docs, and get the same NoSuchBucket error.我还尝试在文档中使用 V1 代码检索存储桶中的所有文件,并得到相同的 NoSuchBucket 错误。
Here's the implementation of that method:这是该方法的实现:
public static void listBuckets(AmazonS3 cosClient, String bucketName) {
System.out.printf("Retrieving bucket contents from: %s\n", bucketName);
ObjectListing objectListing = cosClient.listObjects(new ListObjectsRequest().withBucketName(bucketName));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.printf("Item: %s (%s bytes)\n", objectSummary.getKey(), objectSummary.getSize());
}
System.out.println();
}
So I have tried this code to replicate the error.所以我尝试了这段代码来复制错误。 I was able to list objects in the bucket.
我能够列出存储桶中的对象。 My first guest would be a configuration error.
我的第一个客人是配置错误。
String bucketName = "<BUCKET_NAME>"; // eg my-unique-bucket-name
String newBucketName = "<NEW_BUCKET_NAME>"; // eg my-other-unique-bucket-name
String apiKey = "<API_KEY>"; // eg "W00YiRnLW4k3fTjMB-oiB-2ySfTrFBIQQWanc--P3byk"
String serviceInstanceId = "<SERVICE_INSTANCE_ID"; // eg "crn:v1:bluemix:public:cloud-object-storage:global:a/3bf0d9003abfb5d29761c3e97696b71c:d6f04d83-6c4f-4a62-a165-696756d63903::"
String endpointUrl = "https://s3.us-south.cloud-object-storage.appdomain.cloud"; // this could be any service endpoint
Probably the endpointUrl
is misconfigured.可能
endpointUrl
配置错误。 When you are getting the endpoint follow these steps:获取端点时,请执行以下步骤:
Mine was like this: s3.eu-de.cloud-object-storage.appdomain.cloud
我的是这样的:
s3.eu-de.cloud-object-storage.appdomain.cloud
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.