简体   繁体   English

使用 java spring boot 从 Amazon S3 下载图像

[英]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.我是 AWS 开发的新手,我正在尝试从我的 Amazon S3 存储桶下载作为图像的对象。 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:我正在使用 Spring Boot API,我的代码如下:

storageService.java存储服务.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存储控制器.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.请参阅此 AWS 教程。 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.它讨论了这个确切的用例,并向您展示了如何使用 Spring BOOT 应用程序将位于 Amazon S3 存储桶中的图像下载到您的 Web 浏览器。 For example, this image shows downloading an image named lake.png .例如,此图像显示下载名为Lake.png的图像。

在此处输入图片说明

This app uses the AWS SDK for Java V2.此应用程序使用适用于 Java V2 的 AWS 开发工具包。 See:看:

Creating a dynamic web application that analyzes photos using the AWS SDK for Java 创建使用 AWS SDK for Java 分析照片的动态 Web 应用程序

This example uses the Sync Java Client.此示例使用同步 Java 客户端。 If you prefer using the Java Async client, see:如果您更喜欢使用 Java 异步客户端,请参阅:

Creating a dynamic web application that asynchronously analyzes photos using the AWS SDK for Java 创建一个使用 AWS SDK for Java 异步分析照片的动态 Web 应用程序

I tested this code, works perfectly fine downloading images from S3.我测试了这段代码,从 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只是为了排除是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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