[英]How to unzip files from s3 and save it back on s3
我在S3的存储桶中有一些.zip文件。 我需要解压缩并将其保存回没有本地文件系统的存储桶中。
我知道S3是静态存储,但是我可以通过给s3存储桶路径来解压缩s3本身上的文件。
我有以下问题。
我可以将存储桶/文件夹的路径传递到FileOutputStream(bucketPath)
以便它直接在那里解压缩文件。
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
S3Object.putObject()
也接受inputstream作为参数,我可以将ZipEntry直接转换为InputStream并作为带有元数据的参数进行传递。
我需要使用EMR执行所有操作(本地文件系统不会显示在图片中)。 我可以从s3中读取zip文件并使用EMR解压缩文件并将其保存在S3上吗?
这是我的代码。
S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName,objName)); //sandip.zip
ZipInputStream in = new ZipInputStream(s3object.getObjectContent());
ZipEntry entry=in.getNextEntry(); // sandip_1.graphml
try {
while ((entry!= null)){
s3Client.putObject(bucketName, entry.getName(), new File(entry.getName()));
}
}
catch (IOException e) {
e.printStackTrace();
}
我当前的代码抛出以下异常。
Exception in thread "main" com.amazonaws.AmazonClientException: Unable to calculate MD5 hash: sandip_1.graphml (The system cannot find the file specified)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1319)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1273)
at com.example.testaws.test2.createAdjListZipFiles(Unknown Source)
at com.example.testaws.test1.main(test1.java:33)
Caused by: java.io.FileNotFoundException: sandip_1.graphml (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.amazonaws.util.Md5Utils.computeMD5Hash(Md5Utils.java:97)
at com.amazonaws.util.Md5Utils.md5AsBase64(Md5Utils.java:104)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1316)
... 3 more
请给我提示或参考。
首先,你在一件事情上是对的。 S3是静态存储,因此您不能直接在S3上进行任何文件级更改。 您以某种方式必须下载文件,根据需要进行转换并上传回去。
其次,您绝对可以为此使用EMR。 实际上,它将使您的生活变得非常轻松。 试试看:
创建安装了Hive的EMR群集。
创建一个Hive表,如下所示:创建外部表x {记录字符串}位置's3:// blah';
像上面一样,创建另一个表y,并添加一个附加表:“存储为文本文件”
现在执行“插入覆盖表y从x选择记录”。
在这里,Hive将自动检测输入文件是否已压缩。 之后,您要做的就是指示Hive将相同的数据作为文本文件存储在相同的S3位置。
PS-我无法发布确切的代码或格式正确,因为我在旅途中一直在回答。 但是,我希望您能大致了解。 这肯定会起作用,因为我已经做了几次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.