hello
hello

Reputation: 51

Key for download bucket in Amazon S3 using Java

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();
    }
}

I'm trying to download objects from an Amazon S3 bucket using Java. But it doesn't seem to work and keeps giving me the error shown below. What is the correct key to be input? The access key or the 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

Upvotes: 0

Views: 10039

Answers (4)

Boboman
Boboman

Reputation: 431

As other people pointed out a key is a unique identifier for an object (file/folder etc) stored on S3.

See http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#CoreConcepts for more information on nomenclature and conceptual designs.

Note: If you are trying to upload a file and then immediately do a getObjectRequest, sometimes (most times) there is a delay and would cause a 404 NoSuchKey, so you need to account for that scenario in your design.

A 'key' is NOT the credentials used for the request. You would get a 401 Unauthorized if this was the case. A 404 is indicative of having proper authentication credentials set up.

A key can be considered as a filename (or foldername for that matter). This can be any arbitrary value but most people prefer to mock a file-system structure by using forward slashes (even the Amazon S3 console does this, and has create folder buttons etc.)

If you have access to the console, dig through your buckets and make sure all the key prefixes or 'parent directories' exist, including the bucket or 'root folder'.

Upvotes: 1

8bitme
8bitme

Reputation: 947

I had a similar issue to this. My access key, secret, bucket, endpoint and path were correct.

It turns out that the issue for me was that the file I was pointing to in S3 did not exist hence the 404 error.

Upvotes: 1

Literally Illiterate
Literally Illiterate

Reputation: 359

"Key" in Amazon S3 bucket is the file name you want to download.

In you case:

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

Upvotes: 0

Tej Kiran
Tej Kiran

Reputation: 2248

Make sure the your key which you are using in the code has as it is in the bucket. Remember Amazon S3 keys are case insensitive, means it might be that your key name has some different cases like upper or lower.

Check and try again.

Upvotes: 1

Related Questions