简体   繁体   中英

Downloading images from Amazon S3 using java spring boot

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.

在此处输入图片说明

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?

See this AWS tutorial. 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 .

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM