Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I update an existing Amazon S3 object?

I was looking at Amazon S3 Samples, and the samples are there for inserts/deletes...

But I want to update an existing blob with fresh data. Basically the content is a text file, and the text has been modified, I want the S3 object to store the new text content.

How do I do this in Java?

like image 342
Arvind Avatar asked Mar 01 '12 13:03

Arvind


People also ask

Can we update file in S3 bucket?

It's not possible to append to an existing file on AWS S3. You can delete existing file and upload new file with same name.

How do I update metadata for an existing Amazon S3 file?

You can set object metadata at the time you upload it. After you upload the object, you cannot modify object metadata. The only way to modify object metadata is to make a copy of the object and set the metadata.

Can S3 files be edited?

s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.

What happens to existing S3 objects when you enable versioning?

When you enable S3 Versioning on an existing bucket, objects that are already stored in the bucket are unchanged. The version IDs (null), contents, and permissions remain the same.


1 Answers

Updating an existing object in Amazon S3 doesn't differ from creating it in the first place, i.e. the very same PUT Object operation is used to upload the object and will overwrite the existing one (if it isn't protected by other means, e.g. via Using Bucket Policies or Object Versioning)

You can find a complete code sample in Upload an Object Using the AWS SDK for Java, the main part boils down to:

AmazonS3 s3client = new AmazonS3Client(new PropertiesCredentials(         S3Sample.class.getResourceAsStream(                 "AwsCredentials.properties"))); try {     System.out.println("Uploading a new object to S3 from a file\n");     File file = new File(uploadFileName);     s3client.putObject(new PutObjectRequest(                              bucketName, keyName, file));   } catch (AmazonServiceException ase) {     System.out.println("Caught an AmazonServiceException, which " +             "means your request made it " +             "to Amazon S3, but was rejected with an error response" +             " for some reason.");     // ... error handling based on exception details } 
like image 192
Steffen Opel Avatar answered Oct 12 '22 21:10

Steffen Opel