【问题标题】:How to unzip files from s3 and save it back on s3如何从s3解压缩文件并将其保存回s3
【发布时间】:2016-03-31 10:11:08
【问题描述】:

我在 S3 的存储桶中有一些 .zip 文件。我需要解压缩并需要将其保存回没有本地文件系统的存储桶中。

我知道 S3 是静态存储,但我可以通过提供 s3 存储桶路径在 s3 本身上解压缩文件。

我有以下问题。

  1. 我可以将存储桶/文件夹的路径传递给FileOutputStream(bucketPath),以便它直接在那里解压缩文件。

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));

  2. S3Object.putObject() 也接受 inputstream 作为参数,我可以将 ZipEntry 直接转换为 InputStream 并作为带有元数据的参数传递。

  3. 我需要使用 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

请给我提示或参考。

【问题讨论】:

    标签: java amazon-s3 unzip emr


    【解决方案1】:

    首先,你在一件事情上是对的。 S3 是静态存储,因此您不能直接在 S3 上进行任何文件级更改。您必须以某种方式下载文件,根据需要进行转换并上传回来。

    其次,您绝对可以为此使用 EMR。事实上,它会让你的生活变得非常轻松。试试这个:

    • 创建一个安装了 Hive 的 EMR 集群。

    • 创建一个 Hive 表,有点像这样: 创建外部表 x { 记录字符串 } 位置 's3://blah';

    • 创建另一个名为 y 的表,就像上面一样,添加一个:'stored as textfile'

    • 现在执行“插入覆盖表 y 从 x 中选择记录”。

    在这里,Hive 会自动检测输入文件是否被 gzip 压缩。之后,您所做的就是指示 Hive 将相同的数据存储回相同的 S3 位置,但作为文本文件。

    P.S.- 我无法发布准确的代码或正确的格式,因为我在旅途中回答这个问题。但我希望你能大致了解。这肯定会奏效,因为我已经这样做了几次。

    【讨论】:

    • 这个有更新吗?
    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多