Amazon S3 upload file and get URL

后端 未结 9 1535
醉酒成梦
醉酒成梦 2020-12-23 03:05

Is it possible to upload a txt/pdf/png file to Amazon S3 in a single action, and get the uploaded file URL as the response. If so, is AWS Java SDK the right library that I n

9条回答
  •  攒了一身酷
    2020-12-23 03:09

    Below method uploads file in a particular folder in a bucket and return the generated url of the file uploaded.

    private String uploadFileToS3Bucket(final String bucketName, final File file) {
        final String uniqueFileName = uploadFolder + "/" + file.getName();
        LOGGER.info("Uploading file with name= " + uniqueFileName);
        final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, uniqueFileName, file);
        amazonS3.putObject(putObjectRequest);
        return ((AmazonS3Client) amazonS3).getResourceUrl(bucketName, uniqueFileName);
    }
    

提交回复
热议问题