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?
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.
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.
s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With