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

时间:2012-03-01 13:36:13

标签: java amazon-s3 amazon-web-services

我正在查看Amazon S3样本,样本用于插入/删除...

但我想用新数据更新现有的blob。基本上内容是文本文件,文本已被修改,我希望S3对象存储新的文本内容。

我如何用Java做到这一点?

2 个答案:

答案 0 :(得分:38)

更新Amazon S3中的现有对象与首先创建现有对象没有区别,即使用完全相同的PUT Object操作来上传对象并覆盖现有对象(如果是不受其他方式的保护,例如通过Using Bucket PoliciesObject Versioning

您可以在Upload an Object Using the AWS SDK for Java中找到完整的代码示例,主要部分归结为:

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
}

答案 1 :(得分:0)

您可以使用雅典娜通过INSERT函数更新多余的行。您将需要向Athena提供对S3存储桶的访问权限,使用Amazon Glue对S3存储桶进行爬网以获取表布局,并为模式名称和数据类型更新Glue。完成此操作后,您可以在https://console.aws.amazon.com/athena/home

使用Athena控制台进行更新
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//
//int ModifyText(char Stringboy[], char output[])
//{
//  for(int i=0; Stringboy[i] != '\0'; i++)
//  {
//      if(Stringboy[i] >= 'A' && Stringboy[i] <= 'Z')
//      {
//          output[i] = Stringboy[i] + 32;
//          
//      }
//      else if(Stringboy[i] >= 'a' && Stringboy[i] <= 'z')
//      {
//          output[i] = Stringboy[i];
//      }
//}}

int ModifyText(char *Stringboy, char *output)
{
    while(*Stringboy != '\0')
    {
        if(*Stringboy >= 'A' && *Stringboy <= 'Z')
        {
            *output = *Stringboy + 32;

        }
        else // if(*Stringboy >= 'a' && *Stringboy <= 'z') 
        {
            *output = *Stringboy;
        }
        ++Stringboy;
        ++output;
    }
    *output = '\0';
    return 0;
}

int main(void){
    char samplearray[] = "THE quick Brown Fox jumps over the Lazy Dog!***!"; // braces unneeded.
    char dummy[83];
    printf("Original Text: \n %s\n", samplearray);
    ModifyText(samplearray, dummy);
    printf("Modified Text: \n %s\n", dummy);
    //letterCounter(dummy); //these two bottom functions have their prints written into them, so they need only be called
    //wordCounter(dummy);

    printf("length of sample array is %zd\n", strlen(samplearray));
    return 0;
}