Search code examples
imageamazon-s3downloadgetbucket

Not getting image content in content-disposition after downloading image from S3 Bucket


I am using following code to download the image from AWS S3 bucket.

com.amazonaws.services.s3.model.S3Object object = s3Client.getObject(new 
GetObjectRequest(bucketName, mediaItem.getFileName()));
    S3ObjectInputStream objectData = object.getObjectContent();
    object.close();
    return objectData.toString();

I get 200 response but in imageview it says not an image.


Solution

  • Here is the code to get the object from AWS S3 bucket and write the object stream to servlet response. I have used content disposition as "inline", so it shows the object on browser directly. It doesn't pop up "save as". You can change it to different content disposition based on your requirement.

    Content disposition

    I have used the access key and secret key to get s3 client. You can change that to InstanceProfileCredentialsProvider (i.e. commented code) if you are running the application from EC2 which has access to S3 bucket.

    public class GetS3Object extends HttpServlet {
    
        private static final long serialVersionUID = -3924600769478978726L;
    
        private static Logger logger = Logger.getLogger(GetS3Object.class);
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req, resp);
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            ServletOutputStream out = resp.getOutputStream();
    
            /*AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new InstanceProfileCredentialsProvider(false)).build();*/
    
            BasicAWSCredentials awsCreds = new BasicAWSCredentials("accesskey", "secretkey");
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                    .build();
    
            try {
    
                GetObjectRequest getObjectRequest = new GetObjectRequest("yourbucketName", "objectkey");
                S3Object s3Object = s3Client.getObject(getObjectRequest);
    
                byte[] s3Content = org.apache.commons.io.IOUtils.toByteArray(s3Object.getObjectContent());
    
                resp.setHeader("Content-Type",
                        getServletContext().getMimeType(s3Object.getObjectMetadata().getContentType()));
                resp.setHeader("Content-Length", String.valueOf(s3Content.length));
                resp.setHeader("Content-Disposition", "inline; filename=\"" + "Google_logo.jpeg" + "\"");
    
                out.write(s3Content);
    
            } catch (Exception e) {
                e.printStackTrace();
    
            } finally {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            }
    
        }
    
    }