我可以更新现有的Amazon S3的对象吗?对象、Amazon

2023-09-11 08:22:12 作者:你若不离不弃我必以死相依

我一直在寻找Amazon S3的样品,并且样品在那里为插入/删除...

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

但我想更新与新的数据现有的斑点。基本内容是一个文本文件,该文本进行了修改,我希望S3对象来存储新的文本内容。

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.

我如何做到这一点在Java中?

How do I do this in Java?

推荐答案

更​​新现有对象亚马逊S3 不从第一个地方创建它,即同样的 PUT对象的操作不同的是用于上传的目的和将覆盖现有酮(如果它不是由其他方式,例如通过使用铲斗策略保护或对象版本)

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)

您可以在上传一张完整的code样本对象使用AWS SDK的Java < /一>,主体部分可以归结为:

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
}