Can I update an existing Amazon S3 object?
JavaAmazon S3Amazon Web-ServicesJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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
}
Solution 2 - Java
You can use Athena to update with extra rows via an INSERT function. You will need to provide Athena with access to the S3 bucket, crawl the S3 bucket using Amazon Glue to get the table layout, update Glue for the Schema names and data types. Once this has been done you can then update using the Athena console at https://console.aws.amazon.com/athena/home
INSERT INTO destination_table [(col1,col2,...)]
VALUES (col1value,col2value,...)[,(col1value,col2value,...)][,...]