2

I am new to AWS development and I am trying to download objects that are images from my Amazon S3 bucket. I am able to download objects however they are stored like the image below.

enter image description here

I am using spring boot APIs and my code is here below:

storageService.java

package com.fiado.S3.service;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.util.IOUtils;

@Service
public class storageService {
    
    private String bucketName = "fiadoapp";

    @Autowired 
    private AmazonS3 s3Client;
    
     public byte[] downloadFile(String fileName) {
         try {
              byte[] content;
                final S3Object s3Object = s3Client.getObject(bucketName, fileName);
                final S3ObjectInputStream stream = s3Object.getObjectContent();
                content = IOUtils.toByteArray(stream);
                s3Object.close();
                return content;
            } catch (IOException ioException) {
                ioException.printStackTrace();
            } catch (AmazonServiceException serviceException) {
                serviceException.printStackTrace();
            } catch (AmazonClientException clientException) {
                clientException.printStackTrace();
            }

            return null;
        }
}

storageController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fiado.S3.service.storageService;

@RestController
@RequestMapping("/file")
public class storageController {
    
    @Autowired
    private storageService service;
    
    @GetMapping("/download/{fileName}")
    public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileName) {
        byte[] data = service.downloadFile(fileName);
        
        ByteArrayResource resource = new ByteArrayResource(data);
        
        return ResponseEntity
                .ok()
                .contentLength(data.length)
                .header("Content-type","application/octet-stream")
                .header("content-disposition","attachment; filename=\"" + fileName + "\"")
                .header("Cache-Control", "no-cache")
                .body(resource);
        
    }
    
    
}

Can anyone tell me how I can get my files to be downloaded as images?

smac2020
  • 9,637
  • 4
  • 24
  • 38
Kea501
  • 63
  • 3
  • 11
  • Does this answer your question? [Do I need Content-Type: application/octet-stream for file download?](https://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download) – Chin Huang Nov 16 '21 at 19:23
  • Unfortunately, this did not solve my issue. Thanks for the suggestion though. – Kea501 Nov 16 '21 at 19:49
  • HTTP header names are case-insensitive but it would probably still be a good idea to be consistent. You have 3 headers with 3 different forms of capitalization. Also, when you tested with an appropriate Content-Type e.g. image/png or application/pdf, did it make a difference? – jarmod Nov 17 '21 at 20:44

2 Answers2

1

See this AWS tutorial located in the AWS Code Example Github Repo. It discusses this exact use case and shows you how to download an image located in an Amazon S3 bucket to your web browser using a Spring BOOT app. For example, this image shows downloading an image named lake.png.

enter image description here

This app uses the AWS SDK for Java V2. See:

Creating a dynamic web application that analyzes photos using the AWS SDK for Java

This example uses the Sync Java Client. If you prefer using the Java Async client, see:

Creating a dynamic web application that asynchronously analyzes photos using the AWS SDK for Java

smac2020
  • 9,637
  • 4
  • 24
  • 38
1

I tested this code, works perfectly fine downloading images from S3.

only difference is, can you cross check how you are supplying

fileName

path variable ?

Also, have you tried downloading the file using a standalone program ? just to rule out is

Janardhan
  • 89
  • 3