Amazon S3 upload file and get URL

后端 未结 9 1443
醉酒成梦
醉酒成梦 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:30

    For AWS SDK 2+

    String key = "filePath";
    String bucketName = "bucketName";
    PutObjectResponse response = s3Client
                .putObject(PutObjectRequest.builder().bucket(bucketName ).key(key).build(), RequestBody.fromFile(file));
     GetUrlRequest request = GetUrlRequest.builder().bucket(bucketName ).key(key).build();
     String url = s3Client.utilities().getUrl(request).toExternalForm();
    
    0 讨论(0)
  • 2020-12-23 03:31

    Similarly if you want link through s3Client you can use below.

    System.out.println("filelink: " + s3Client.getUrl("your_bucket_name", "your_file_key"));
    
    0 讨论(0)
  • 2020-12-23 03:32

    To make the file public before uploading you can use the #withCannedAcl method of PutObjectRequest:

    myAmazonS3Client.putObject(new PutObjectRequest('some-grails-bucket',  'somePath/someKey.jpg', new    File('/Users/ben/Desktop/photo.jpg')).withCannedAcl(CannedAccessControlList.PublicRead))
    
    0 讨论(0)
提交回复
热议问题