Skip to content

Instantly share code, notes, and snippets.

@h-hub
Last active October 18, 2020 05:00
Show Gist options
  • Save h-hub/73e7a0932bf9e452e298df1b8be293d8 to your computer and use it in GitHub Desktop.
Save h-hub/73e7a0932bf9e452e298df1b8be293d8 to your computer and use it in GitHub Desktop.

Revisions

  1. h-hub revised this gist Oct 18, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ImageService.java
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ public String uploadImage(MultipartFile image, String bucketName) {
    try {
    bimg = ImageIO.read(imageFile);
    } catch (IOException e) {
    throw new FileUploadException("AvatarImgValidator: Unable to read image.");
    throw new FileUploadException("Unable to read image.");
    }

    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-southeast-1").build();
  2. h-hub revised this gist Oct 18, 2020. 1 changed file with 32 additions and 32 deletions.
    64 changes: 32 additions & 32 deletions ImageService.java
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,34 @@
    public String uploadImage(MultipartFile image, String bucketName) {

    File imageFile;

    try {
    imageFile = convertMultiPartToFile(image);
    } catch (IOException e) {
    throw new FileUploadException("Unable to convert input stream to file.");
    }

    BufferedImage bimg = null;
    try {
    bimg = ImageIO.read(imageFile);
    } catch (IOException e) {
    throw new FileUploadException("AvatarImgValidator: Unable to read image.");
    }

    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-southeast-1").build();
    TransferManager xfer_mgr = TransferManagerBuilder.standard().withS3Client(s3client).build();
    String fileName = generateFileName(image);

    try {
    Upload xfer = xfer_mgr.upload(new PutObjectRequest(bucketName, fileName, imageFile)
    .withCannedAcl(CannedAccessControlList.PublicRead));

    xfer.waitForCompletion();

    } catch (AmazonServiceException | InterruptedException e) {
    throw new FileUploadException("AWS Error or Upload was interrupted");
    }
    xfer_mgr.shutdownNow();

    return "https://"+bucketName+".s3-ap-southeast-1.amazonaws.com/"+fileName;
    }
    File imageFile;

    try {
    imageFile = convertMultiPartToFile(image);
    } catch (IOException e) {
    throw new FileUploadException("Unable to convert input stream to file.");
    }

    BufferedImage bimg = null;
    try {
    bimg = ImageIO.read(imageFile);
    } catch (IOException e) {
    throw new FileUploadException("AvatarImgValidator: Unable to read image.");
    }

    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-southeast-1").build();
    TransferManager xfer_mgr = TransferManagerBuilder.standard().withS3Client(s3client).build();
    String fileName = generateFileName(image);

    try {
    Upload xfer = xfer_mgr.upload(new PutObjectRequest(bucketName, fileName, imageFile)
    .withCannedAcl(CannedAccessControlList.PublicRead));

    xfer.waitForCompletion();

    } catch (AmazonServiceException | InterruptedException e) {
    throw new FileUploadException("AWS Error or Upload was interrupted");
    }
    xfer_mgr.shutdownNow();

    return "https://"+bucketName+".s3-ap-southeast-1.amazonaws.com/"+fileName;
    }
  3. h-hub created this gist Oct 17, 2020.
    34 changes: 34 additions & 0 deletions ImageService.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    public String uploadImage(MultipartFile image, String bucketName) {

    File imageFile;

    try {
    imageFile = convertMultiPartToFile(image);
    } catch (IOException e) {
    throw new FileUploadException("Unable to convert input stream to file.");
    }

    BufferedImage bimg = null;
    try {
    bimg = ImageIO.read(imageFile);
    } catch (IOException e) {
    throw new FileUploadException("AvatarImgValidator: Unable to read image.");
    }

    AmazonS3 s3client = AmazonS3ClientBuilder.standard().withRegion("ap-southeast-1").build();
    TransferManager xfer_mgr = TransferManagerBuilder.standard().withS3Client(s3client).build();
    String fileName = generateFileName(image);

    try {
    Upload xfer = xfer_mgr.upload(new PutObjectRequest(bucketName, fileName, imageFile)
    .withCannedAcl(CannedAccessControlList.PublicRead));

    xfer.waitForCompletion();

    } catch (AmazonServiceException | InterruptedException e) {
    throw new FileUploadException("AWS Error or Upload was interrupted");
    }
    xfer_mgr.shutdownNow();

    return "https://"+bucketName+".s3-ap-southeast-1.amazonaws.com/"+fileName;
    }