gpt4 book ai didi

amazon-web-services - 更新 Amazon S3 存储桶中的文件

转载 作者:行者123 更新时间:2023-12-01 08:26:38 25 4
gpt4 key购买 nike

我正在尝试将一个字符串附加到存储在 S3 中的文本文件的末尾。
目前我只是将文件的内容读入一个字符串,附加我的新文本并将文件重新保存回 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));
}

[/代码]

最佳答案

无法附加到 AWS S3 上的现有文件。当您上传一个对象时,它会创建一个新版本(如果它已经存在):

If you upload an object with a key name that already exists in the bucket, Amazon S3 creates another version of the object instead of replacing the existing object



来源: 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

关于amazon-web-services - 更新 Amazon S3 存储桶中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33106248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com