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

mi7gmzs6  于 2023-10-15  发布在  Spring
关注(0)|答案(3)|浏览(153)

我是AWS开发的新手,我正在尝试从我的Amazon S3存储桶中下载图像对象。我可以下载对象,但它们是像下面的图像存储。

我正在使用Sping Boot API,我的代码如下:
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);
        
    }
    
    
}

谁能告诉我如何将我的文件下载为图像?

tv6aics1

tv6aics11#

我测试了这段代码,从S3下载图像工作得很好.
唯一的区别是,你能交叉检查你是如何提供的吗?
文件名
路径变量?
此外,你有没有尝试下载文件使用一个独立的程序?只是为了排除

fjnneemd

fjnneemd2#

请参阅位于AWS Code Example Github Repo中的AWS教程。它讨论了这个确切的用例,并向您展示了如何使用Sping Boot 应用程序将位于Amazon S3存储桶中的图像下载到您的Web浏览器。例如,此图像显示正在下载名为lake.png的图像。

此应用程序使用AWS SDK for Java V2。请参阅:
Creating a dynamic web application that analyzes photos using the AWS SDK for Java
此示例使用Sync Java客户端。如果您更喜欢使用Java Async客户端,请参阅:
Creating a dynamic web application that asynchronously analyzes photos using the AWS SDK for Java

h9a6wy2h

h9a6wy2h3#

如果你需要访问对象url,你需要使bucket策略公开
在权限选项卡->滚动到存储桶策略并添加此

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucketname/*"
        }
    ]
}

相关问题