[英]How to load zip file that resides in S3 bucket?
I have a situation where I need to open a zip file that resides in S3 bucket. 我遇到一种情况,我需要打开一个S3存储桶中的zip文件。 So far my code is like below:
到目前为止,我的代码如下所示:
public ZipFile readZipFile(String name) throws Exception {
GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
S3Object obj = s3Client.getObject(req);
S3ObjectInputStream is = obj.getObjectContent();
/******************************
* HOW TO DO
******************************/
return null;
}
Previously I did try creating a temporary file object and with File.createTempFile function, but I always got trouble where I don't get the File object created. 以前,我确实尝试过使用File.createTempFile函数创建一个临时文件对象,但是在没有创建File对象的地方总是遇到麻烦。 My previous attempt was like below:
我以前的尝试如下:
public ZipFile readZipFile(String name) throws Exception {
GetObjectRequest req = new GetObjectRequest(settings.getAwsS3BatchRecogInBucketName(), name);
S3Object obj = s3Client.getObject(req);
S3ObjectInputStream is = obj.getObjectContent();
File temp = File.createTempFile(name, "");
temp.setWritable(true);
FileOutputStream fos = new FileOutputStream(temp);
fos.write(IOUtils.toByteArray(is));
fos.flush();
return new ZipFile(temp);
}
Anybody ever got into this situation? 有人遇到过这种情况吗? Please advice me thanks :)
请告诉我谢谢:)
If you want to use the zip file immediately without saving it to a temporary file first, you can use java.util.zip.ZipInputStream
: 如果要立即使用zip文件而不先将其保存到临时文件中,则可以使用
java.util.zip.ZipInputStream
:
import java.util.zip.ZipInputStream;
S3ObjectInputStream is = obj.getObjectContent();
ZipInputStream zis = new ZipInputStream(is);
From there on you can read through the entries of the zip files, ignoring the ones that you don't need, and using the ones that you need: 从那里,您可以通读zip文件的条目,忽略不需要的文件,并使用所需的文件:
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
String name = entry.getName();
if (iWantToProcessThisEntry(name)) {
processFile(name, zis);
}
zis.closeEntry();
}
public void processFile(String name, InputStream in) throws IOException { /* ... */ }
You don't need to worry about storing temporary files that way. 您无需担心以这种方式存储临时文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.