如何将存储在mysql中的图片作为blob上传到AWS S3 bucket中?

问题描述 投票:0回答:1

我已经存储在MySQL表中的员工照片作为BLOB类型的字段与员工ID。我必须将这些BLOB图像作为图像文件迁移到S3 Bucket。我正在重新恢复输入流对象的图像在java。现在如何获得图像的扩展和内容类型的图像。如何将其上传到S3 bucket

java mysql amazon-s3 inputstream
1个回答
0
投票

我不知道你是否可以识别图像扩展名,除非你把它保存到文件系统,因为它是保存为一个blob。这里是一个示例代码,你可以使用这样的东西

AmazonS3 client = AmazonS3ClientBuilder.defaultClient();
    //Load the blob as a byte array
    byte [] imageData = <load the image data from db> ;// resultSet.getBytes("image");
    //create a input stream from byte array
    ByteArrayInputStream inputStream = new ByteArrayInputStream(imageData);
    //Create the buffered image from input stream
    BufferedImage image = ImageIO.read(inputStream);
    //File which you are going to save the image as
    File file = new File(String.format("$s/%s.png", folderLocation, fileName));
    //Regardless of your original image extension save the image as png as it's loseless 
    ImageIO.write(image, "PNG", file);
    //Create a s3 put request 
    PutObjectRequest request = new PutObjectRequest(bucketName, UUID.randomUUID().toString()+".png", file);
    //Upload to s3
    client.putObject(request);
© www.soinside.com 2019 - 2024. All rights reserved.