简体   繁体   中英

How to Upload Image stored as blob in mysql to AWS S3 bucket

I have stored employee photo in MySQL table as BLOB type field with employee id. I have to migrate those blob image as image files to S3 Bucket. I am retriving InputStream object for that image in java. Now how to get extension of image and contenttype of that image. How to upload it to S3 bucket

I'm not sure if you can recognize image extension unless you save it to the file system since it's saved as a blob. Here is a sample code which you can use for something like this

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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