Objects

Uploading a file to a bucket

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

Get Amazon S3 Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.