To upload a file to a bucket, we will first create a bucket, and then upload the object into the bucket. Along with uploading the object, we need to assign the access control list (ACL) so that a particular user group can access the object. Here we will set the ACL as PublicRead
:
s3.putObject(new PutObjectRequest( bucketName, fileName, new File(filePath)) .withCannedAcl(CannedAccessControlList.PublicRead));
There are other canned access control lists, such as:
AuthenticatedRead
BucketOwnerFullControl
BucketOwnerRead
LogDeliveryWrite
Private
PublicRead
PublicReadWrite
The complete code to upload a file to a bucket is as follows:
package com.chapter3; import java.io.File; import java.util.Date; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model...