Amazon S3中使用Java的下载桶的密钥

wh6knrhe  于 2023-02-28  发布在  Java
关注(0)|答案(4)|浏览(221)
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 bucket下载对象。但似乎不起作用,并不断给我以下所示的错误。输入正确的密钥是什么?访问密钥还是密钥?

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
7gcisfzg

7gcisfzg1#

确保你在代码中使用的键和桶中的键一样。记住Amazon S3键不区分大小写,这意味着你的键名可能有一些不同的大小写,比如大写或小写。
请检查并重试。

fbcarpbf

fbcarpbf2#

我有一个类似的问题。我的访问密钥,秘密,桶,端点和路径是正确的。
原来,对我来说,问题是我在S3中指向的文件不存在,因此出现了404错误。

hc2pp10m

hc2pp10m3#

正如其他人所指出的,是存储在S3上的对象(文件/文件夹等)的唯一标识符
有关http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#CoreConcepts术语和概念设计的详细信息,请访问www.example.com。

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

“密钥”不是用于请求的身份证明。如果是这种情况,您将得到***401未授权***。404表示已设置正确的验证身份证明。
键可以被看作是文件名(或文件夹名),可以是任意值,但大多数人喜欢使用正斜杠来模拟文件系统结构(甚至Amazon S3控制台也这样做,并有创建文件夹按钮等)。
如果您可以访问控制台,请仔细检查您的存储桶,确保所有关键字前缀或“父目录”都存在,包括存储桶或“根文件夹”。

qfe3c7zg

qfe3c7zg4#

Amazon S3桶中的“Key”是您要下载的文件名。
在您的情况下:

private static String key        = "__________________________________"; //will be file name/ file path to that file in Amazon Bucket.

相关问题