使用Java中的URL从加密的S3存储桶中检索图像

时间:2018-03-28 19:22:38

标签: java spring spring-boot amazon-s3

private static final String bucketName = "imagebucket";
private static final BasicAWSCredentials credentials = new BasicAWSCredentials("secret_id", "secret_pass");
private static final String destinationFolder = "images/";

private static AmazonS3 s3 = AmazonS3Client
                    .builder()
                    .withRegion("us-east-2")
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .build();

@RequestMapping("/image")
public String uploadToBucketAndReturnUrl(byte[] fileByteArray, String extension)
{
    //generate a UUID hash
    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();
    String imageHash = randomUUIDString + "." + extension;
    String filePath = destinationFolder+imageHash;

    try
    {
        File tf = File.createTempFile("image", "." + extension); 
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(fileByteArray));
        ImageIO.write(image, extension, tf);
        s3.putObject(new PutObjectRequest(bucketName, filePath, tf));   
    }

    catch(IOException e)
    {
        e.printStackTrace();
    }
    //return the url
    String imageUrl = "https://s3.us-east-2.amazonaws.com/" + bucketName + "/" + filePath;
    return imageUrl;
}   

我有一个功能,可以将图像上传到我的加密存储桶并返回一个URL。现在我需要创建一个函数来接收URL并从存储桶中检索图像,我不确定如何处理。

0 个答案:

没有答案