简体   繁体   English

从S3存储桶下载压缩的.gz文件中的文件

[英]Download the files inside compressed .gz files from S3 Bucket

I have a set of .gz compressed files in s3 bucket. 我在s3存储桶中有一组.gz压缩文件。 I want to download the csv file inside the .gz file. 我想在.gz文件中下载csv文件。 I tried to extract the .gz file and put it into the s3Object. 我试图提取.gz文件并将其放入s3Object。 Now i need to extract the s3 object and download the csv file inside it using java. 现在,我需要提取s3对象并使用java下载其中的csv文件。 Please advise.This is the code I used.Now i am able to download gz file. 请指教。这是我使用的代码。现在,我可以下载gz文件了。 But I need to download csv file inside gz. 但是我需要在gz中下载csv文件。

S3Object object = s3Client.getObject(“bucket”,“Location/file.gz”);
final String encoding = null;
return ResponseEntity.ok(IOUtils.toString(object.getObjectContent(), encoding));

I need help in unzipping the gz file in s3object and return the decompressed contents in the response. 我需要在s3object中解压缩gz文件并在响应中返回解压缩的内容的帮助。

The below code will convert your gunzip file into plain data, but I'm not sure 100% about your actual issue, whether you want to display the content in browser itself or you want to send it as Save as Option , that's why I did a minimum code change to your code assuming you have only problem in converting gunzip format to CSV data, hope you could modify/enhance it that suits you best. 下面的代码会将您的gunzip文件转换为纯数据,但是我不确定您的实际问题是100%,无论您是要在浏览器中显示内容还是要将其发送为Save as Option ,这就是我这样做的原因假设您在将gunzip格式转换为CSV数据时仅遇到问题,则对代码进行最少的代码更改,希望您可以对其进行修改/增强以使其最适合您。

import java.util.zip.GZIPInputStream;
//your method begins from here
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();


S3Object object = s3.getObject("your-bucket", "file-path");


return ResponseEntity.ok(IOUtils.toString(new GZIPInputStream(object.getObjectContent())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM