Nick Nagorski
Nick Nagorski

Reputation: 115

Best way to retrieve files from AWS S3 with Spring Boot?

I'm working on spring boot app that using PostgreSQL as a database and S3 bucket as a storage for images. When saving new entry I'm uploading image to S3 bucket and saving its name to DB. To retrieve the image from S3 bucket I need to prepend URL pattern in view like this:

<img th:src="@{'https://s3.us-east-2.amazonaws.com/MYBUCKETNAME/test/'+${entry.image}}" height="256" width="256"/>

BUT, to see the image i need to make all incoming file for "test" folder public by adding new policy to S3 bucket like this:

{
"Version": "2008-10-17",
"Statement": [
    {
        "Sid": "AllowPublicRead",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::MYBUCKETNAME/test/*"
    }
]

}

QUESTIONS:

  1. Is this a save approach?
  2. Can I retrieve files in a different way?

Upvotes: 2

Views: 3450

Answers (2)

Deepak Singhal
Deepak Singhal

Reputation: 10874

You are doing just fine. All you need to make sure is that do NOT give permission to list contents of buckets viz. if we type https://s3.us-east-2.amazonaws.com/MYBUCKETNAME/test/ on browser it should NOT list all file names in this folder.

As mentioned by Mohan; you can also configure a CDN (in front of your s3 bucket) to retrieve your files; but that is ONLY to improve load time for your customers.. but that is extra configuration and cost involved and is ONLY useful if you have lots of geographically distributed customers.

Upvotes: 3

Mohan Shanmugam
Mohan Shanmugam

Reputation: 690

You Can configure CDN for your S3 bucket and serve the image directly from Edge Location.

Read about AWS CDN

https://aws.amazon.com/caching/cdn/

When you create a CloudFront distribution and specify MYBUCKETNAME.s3.amazonaws.com as the origin server for this distribution. CloudFront returns d111111abcdef8.cloudfront.net as url for s3 bucket.

You can get more information from the below link

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LinkFormat.html#Format_BasicLinks

Upvotes: 2

Related Questions