0
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 存储桶下载对象。但它似乎不起作用,并不断给我下面显示的错误。要输入的正确键是什么?访问密钥还是密钥?

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
4

4 回答 4

1

正如其他人指出的那样,密钥是存储在 S3 上的对象(文件/文件夹等)的唯一标识符

有关命名法和概念设计的更多信息,请参阅http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#CoreConcepts

注意:如果您尝试上传文件然后立即执行 getObjectRequest,有时(大多数情况下)会有延迟并会导致 404 NoSuchKey,因此您需要在设计中考虑这种情况。

“密钥”不是用于请求的凭据。如果是这种情况,您将获得401 Unauthorized 。404 表示已设置正确的身份验证凭证。

可以将密钥视为文件名(或文件夹名)。这可以是任意值,但大多数人更喜欢使用正斜杠来模拟文件系统结构(甚至 Amazon S3 控制台也这样做,并具有创建文件夹按钮等)

如果您有权访问控制台,请浏览您的存储桶并确保所有键前缀或“父目录”都存在,包括存储桶或“根文件夹”。

于 2014-05-19T22:18:56.293 回答
1

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

检查并重试。

于 2012-07-20T06:10:58.870 回答
0

Amazon S3 存储桶中的“Key”是您要下载的文件名。

在你的情况下:

private static String key        = "__________________________________"; //will be file name/ file path to that file in Amazon Bucket.
于 2013-10-17T07:55:22.667 回答
0

我有一个类似的问题。我的访问密钥、秘密、存储桶、端点和路径是正确的。

事实证明,对我来说问题是我在 S3 中指向的文件不存在,因此出现 404 错误。

于 2014-02-03T09:50:10.083 回答