Serving Private Files From S3 Bucket – AWS in Java

If you are building any kind of application, you will most likely need to store and display images or other types of files. This tutorial will quickly show you how to access private images on your S3 Bucket.

By default, if you try to access an image via URL, that is not publicly available you will see something like this:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>9B7D24726707AB3AEXAMPLE</RequestId>
<HostId>
iCES7s/9oSmghFCVRa5sC470Do89p8cEXAMPLE=
</HostId>
</Error>

In order to serve your image to the front end without having to make it public you will need to use your IAM user credentials (Access Key Id and Secret Access Key) to generate a temporary URL and pass it to the front end.

In Java, to do this, you could write a simple method like so:

public static URL generatePresignedUrlRequest(String image){
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
        AmazonS3 s3client = new AmazonS3Client(awsCreds);
 
        java.util.Date expiration = new java.util.Date();
        long msec = expiration.getTime();
        msec += 1000 * 60 * 60; // 1 hour.
        expiration.setTime(msec);
 
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(BUCKET_NAME, image);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
        generatePresignedUrlRequest.setExpiration(expiration);
 
        URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);
 
        return s;
    }

The method takes in the Object Key (aka filename as defined in your bucket), and generates a URL that is valid for a set period of time (in this case 1 hour).
This keeps your images secure and does not give the public access to them without your secret keys!

Entrepreneurs – Creating Your First Mobile App

So you came up with a great app idea and have been thinking about it for a while now but aren’t sure where to start. I am writing this article from my personal experiences of being involved in the field, in hopes to guide you in the right direction.

Determine platform to support

After the idea stage, you will need to start planning what your target market is, and what will be the best avenue to reach that target market. In other words: Is your target market mainly an Android crowd, Apple crowd, Blackberry or a combination of them?

Mockups/Wireframe

The next step is to begin planning out your app on paper or using a mockup tool. This will help you visualize your app and it will help you when approaching developers to explain your idea. These mockups do not have to be very detailed, just some basic screen layouts will suffice. Below is an example of a wireframe in its beginning stages; a good group of developers can take these and provide suggestions based on their experience in the field.

wireframe example
Continue reading