Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
");
    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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...