gpt4 book ai didi

java - S3文件上传困惑

转载 作者:行者123 更新时间:2023-11-30 01:48:11 28 4
gpt4 key购买 nike

我正在我的 S3 存储桶上保存文件,但我注意到为此我使用 FileOutPutStream ,如下所示:

private UploadedFile file; // This is from PrimeFaces, the file that the client wishes to upload
File uploadedFile = new File(file.getFileName()); // Leaving the file like this creates the file on my IDE folder AFTER executing the next two lines, thats why I though the next lines were an error.

FileOutputStream fileOutput = new FileOutputStream(uploadedFile);
fileOutput.write(file.getContents());

所以这行代码负责在我的设备上写入文件,我首先认为这是一个错误或者没有必要,因为我对文件上传到亚马逊不太了解,所以我删除了这两行因为我注意到我的上传方法只需要文件和文件名,如下所示:

businessDelegatorView.uploadPublicRead("mybucketname", fileName, fileToUpload);

所以我认为这没有必要,那只是复制文件:

FileOutputStream fileOutput = new FileOutputStream(uploadedFile);
fileOutput.write(file.getContents());

但我注意到,如果我删除它们,上传将不起作用,因为它会抛出 FileNotFoundException 所以我开始搜索并找出 this post从 BalusC 中我得到它,我必须定义一个路径,其中来 self 的客户端的文件将被保存以便以后上传,例如在本例中上传到亚马逊 s3 存储桶,但我想知道,例如,当 .生成 WAR:

File uplodadFile = new File("C:/xampp/apache/conf", file.getFileName());

FileOutputStream fileOutput = new FileOutputStream(uploadFile);
fileOutput.write(file.getContents());

我将文件保存在那里作为测试,但我不知道或不确定 FileOutPutStream 是否是正确的选择,我不知道其他方法。

这也是执行上述代码后上传方法的样子,如果没有FileOutPutStream,它将无法工作,因为文件不在我的设备中

AmazonS3 amazonS3 = buildAmazonS3(); 
try {
amazonS3.putObject(new PutObjectRequest(bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead));

只是希望有人为我澄清一些事情,比如这里最好的路径是什么?

File uplodadFile = new File("C:/xampp/apache/conf", file.getFileName());

或者这并不重要,我只需要记住 .WAR 将部署在哪台机器上?谢谢

最佳答案

Just want somebody to clear things a little bit more for me, like what is the best path to put on here?

当您想要将文件上传到系统中时,请尽可能将其保留为字节流,因为您在条目中接收字节并且希望在末尾存储这些相同的字节。转换字节->文件->字节非常耗时、消耗资源并且容易出错(编码转换和存储在文件系统上的文件可能确实是错误源)。

So I though this wasn't necessary and that was only duplicating the files:

FileOutputStream fileOutput = new FileOutputStream(uploadedFile);
fileOutput.write(file.getContents());

你是对的,因为该文件已经通过客户端 HTTP 请求上传。
做两次看起来很无奈。
但是你没有 File 而是一个 UploadedFile (primefaces)

PutObjectRequest() S3 API 的构造函数有多个重载。
实际上你使用它:

public PutObjectRequest(String bucketName,
String key,
File file)

最后一个参数是一个文件。你看到不匹配的地方了吗?在第一个让您烦恼的代码中,您通过 writing the content of the UploadedFile into a new File 解决了问题(当您有 UploadedFile 作为源时传递 File)。如果您需要文件,这是可以接受的。
但实际上您不需要文件,因为 PutObjectRequest()构造函数有另一个 overload更适合您的用途:

public PutObjectRequest(String bucketName,
String key,
InputStream input,
ObjectMetadata metadata)

Constructs a new PutObjectRequest object to upload a stream of data to the specified bucket and key. After constructing the request, users may optionally specify object metadata or a canned ACL as well.

请注意,为了不影响性能,提供内容长度很重要:

Content length for the data stream must be specified in the object metadata parameter; Amazon S3 requires it be passed in before the data is uploaded. Failure to specify a content length will cause the entire contents of the input stream to be buffered locally in memory so that the content length can be calculated, which can result in negative performance problems.

所以你可以这样做:

UploadedFile file = ...; // uploaded by client
ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentLength(file.getSize());
amazonS3.putObject(new PutObjectRequest(bucketName, key, file.getInputStream(), metaData)
.withCannedAcl(CannedAccessControlList.PublicRead));

关于java - S3文件上传困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130822/

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