Home > Net >  Downloading images from Amazon S3 using java spring boot
Downloading images from Amazon S3 using java spring boot

Time:11-18

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?

CodePudding user response:

See this AWS tutorial located in the 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

CodePudding user response:

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

  • Related