gpt4 book ai didi

java - 使用 Java 在 Amazon S3 中下载存储桶的 key

转载 作者:行者123 更新时间:2023-12-02 07:41:07 24 4
gpt4 key购买 nike

public class downloads3 {
private static String bucketName = "s3-upload-sdk-sample-akiaj6ufcgzvw7yukypa";
**private static String key = "__________________________________";**

public static void main(String[] args) throws IOException {
AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
downloads3.class.getResourceAsStream(
"AwsCredentials.properties")));
try {
System.out.println("Downloading an object");
S3Object s3object = s3Client.getObject(new GetObjectRequest(
bucketName, key));
System.out.println("Content-Type: " +
s3object.getObjectMetadata().getContentType());
displayTextInputStream(s3object.getObjectContent());

// Get a range of bytes from an object.

GetObjectRequest rangeObjectRequest = new GetObjectRequest(
bucketName, key);
rangeObjectRequest.setRange(0, 10);
S3Object objectPortion = s3Client.getObject(rangeObjectRequest);

System.out.println("Printing bytes retrieved.");
displayTextInputStream(objectPortion.getObjectContent());

} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which" +
" means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"+
" the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}

private static void displayTextInputStream(InputStream input)
throws IOException {
// Read one text line at a time and display.
BufferedReader reader = new BufferedReader(new
InputStreamReader(input));
while (true) {
String line = reader.readLine();
if (line == null) break;

System.out.println(" " + line);
}
System.out.println();
}
}

我正在尝试使用 Java 从 Amazon S3 存储桶下载对象。但它似乎不起作用,并不断给我带来如下所示的错误。正确的输入键是什么?访问 key 还是 secret key ?

Downloading an object
Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.
Error Message: The specified key does not exist.
HTTP Status Code: 404
AWS Error Code: NoSuchKey
Error Type: Client
Request ID: F9548FC068DB1646

最佳答案

确保您在代码中使用的 key 与存储桶中的 key 相同。请记住,Amazon S3 key 不区分大小写,这意味着您的 key 名称可能有一些不同的大小写,例如大写或小写。

检查并重试。

关于java - 使用 Java 在 Amazon S3 中下载存储桶的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11572451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com