在Amazon S3存储桶中更新文件

May*_*Man 3 amazon-s3 amazon-web-services

我试图将一个字符串附加到存储在S3中的文本文件的末尾.目前我只是将文件的内容读入一个String,追加我的新文本并将文件重新保存回S3.有一个更好的方法吗.我想,当文件是>>> 10MB然后读取整个文件不是一个好主意所以我该如何正确地做到这一点?

当前代码[代码]

private void saveNoteToFile( String p_note ) throws IOException, ServletException    
{
    String str_infoFileName =  "myfile.json"; 

    String existingNotes = s3Helper.getfileContentFromS3( str_infoFileName  ); 
    existingNotes += p_note;
    writeStringToS3( str_infoFileName , existingNotes );        
}

public void writeStringToS3(String p_fileName, String p_data) throws IOException 
{
  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( p_data.getBytes());

  try {
      streamFileToS3bucket(  p_fileName, byteArrayInputStream, p_data.getBytes().length);
  } 
  catch (AmazonServiceException e)
  {
      e.printStackTrace();
  } catch (AmazonClientException e)
  {
      e.printStackTrace();
  }
}

public void streamFileToS3bucket( String p_fileName,  InputStream input, long size)
{
    //Create sub folders if there is any in the file name.
    p_fileName = p_fileName.replace("\\", "/");
    if( p_fileName.charAt(0) == '/')
    {
        p_fileName = p_fileName.substring(1, p_fileName.length());
    }
    String folder = getFolderName( p_fileName );
    if( folder.length() > 0)
    {
        if( !doesFolderExist(folder))
        {
            createFolder( folder );
        }
    }
    ObjectMetadata metadata =  new ObjectMetadata();
    metadata.setContentLength(size);
    AccessControlList acl = new AccessControlList();
    acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);

    s3Client.putObject(new PutObjectRequest(bucket, p_fileName , input,metadata).withAccessControlList(acl));
}
Run Code Online (Sandbox Code Playgroud)

[/码]

Vol*_*soy 10

无法附加到AWS S3上的现有文件.上传对象时,如果已存在,则会创建新版本:

如果您上传具有存储桶中已存在的密钥名称的对象,则Amazon S3会创建该对象的另一个版本,而不是替换现有对象

资料来源:http://docs.aws.amazon.com/AmazonS3/latest/UG/ObjectOperations.html

对象是不可变的.

这些AWS论坛主题中也提到了这一点:

https://forums.aws.amazon.com/message.jspa?messageID=179375 https://forums.aws.amazon.com/message.jspa?messageID=540395