我在MySQL表中将员工照片存储为具有员工ID的BLOB类型字段。我必须将这些Blob图像作为图像文件迁移到S3存储桶。我正在用Java检索该图像的InputStream对象。现在如何获取图像的扩展名和该图像的内容类型。如何将其上传到S3存储桶
答案 0 :(得分: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);