O'Reilly logo

Amazon S3 Essentials by Sunil Gulabani

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required