如何从 S3 下载 GZip 文件?
How to download GZip file from S3?
我都看过 AWS S3 Java SDK - Download file help and Working with Zip and GZip files in Java。
虽然它们分别提供了从 S3 和 GZipped 文件下载和处理文件的方法,但它们无助于处理位于 S3 中的 GZipped 文件。我该怎么做?
目前我有:
try {
AmazonS3 s3Client = new AmazonS3Client(
new ProfileCredentialsProvider());
String URL = downloadURL.getPrimitiveJavaObject(arg0[0].get());
S3Object fileObj = s3Client.getObject(getBucket(URL), getFile(URL));
BufferedReader fileIn = new BufferedReader(new InputStreamReader(
fileObj.getObjectContent()));
String fileContent = "";
String line = fileIn.readLine();
while (line != null){
fileContent += line + "\n";
line = fileIn.readLine();
}
fileObj.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return "ERROR IOEXCEPTION";
}
显然,我没有处理文件的压缩性质,我的输出是:
����sU�3204�50�5010�20�24��L,(���O�V�M-.NLOU�R�U�����<s��<#�^�.wߐX�%w���������}C=�%�J3��.�����둚�S�ᜑ���ZQ�T�e��#sr�cdN#瘐:&�
S�BǔJ����P�<��
但是我无法实现上面给出的second question中的示例,因为文件不在本地,需要从S3下载。
我该怎么办?
我使用 Scanner
而不是 InputStream
解决了这个问题。
扫描器获取 GZIPInputStream 并逐行读取解压缩的文件:
fileObj = s3Client.getObject(new GetObjectRequest(oSummary.getBucketName(), oSummary.getKey()));
fileIn = new Scanner(new GZIPInputStream(fileObj.getObjectContent()));
我并不是在寻找这个问题,但我确实想通过实际解释为什么已经提供的解决方案有效来提高这个线程的质量。
不,这不是因为建议的扫描仪。这是因为通过将 fileObj.getObjectContent()
包装在解压缩内容的 GZIPInputStream
中,流被 解压 。
删除 scanner
但保留 GZIPInputStream
,一切仍然有效。
您必须使用 GZIPInputStream
来读取 GZIP 文件
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.build();
String URL = downloadURL.getPrimitiveJavaObject(arg0[0].get());
S3Object fileObj = s3Client.getObject(getBucket(URL), getFile(URL));
byte[] buffer = new byte[1024];
int n;
FileOutputStream fileOuputStream = new FileOutputStream("temp.gz");
BufferedInputStream bufferedInputStream = new BufferedInputStream( new GZIPInputStream(fileObj.getObjectContent()));
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOuputStream);
while ((n = bufferedInputStream.read(buffer)) != -1) {
gzipOutputStream.write(buffer);
}
gzipOutputStream.flush();
gzipOutputStream.close();
请尝试通过这种方式从 S3 下载 GZip 文件。
试试这个
BasicAWSCredentials creds = new BasicAWSCredentials("accessKey", "secretKey");
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds))
.withRegion(Regions).build();
String bucketName = "bucketName";
String keyName = "keyName";
S3Object fileObj = s3.getObject(new GetObjectRequest(bucketName, keyName));
Scanner fileIn = new Scanner(new GZIPInputStream(fileObj.getObjectContent()));
if (null != fileIn) {
while (fileIn.hasNext()) {
System.out.println("Line: " + fileIn.nextLine());
}
}
}
我正在努力使用 SDK 2.x 实现相同的目标。随着 SDK 2 中引入的新理念,我不得不在找到解决方案之前做一些研究。因此,为了使用 SDK 2.0 的人们的利益,在这里添加一个代码片段。
S3Client s3 = S3Client.builder()
.region(region)
.build();
//Using the key, get the object
GetObjectRequest request = GetObjectRequest.builder().bucket(bucketName).key(key).build();
//Read the object as input stream
InputStream inputStream = s3.getObject(request, ResponseTransformer.toBytes()).asInputStream();
final GZIPInputStream zipInputStream;
try {
//Convert it to GZIP stream
zipInputStream = new GZIPInputStream(inputStream);;
BufferedReader in = new BufferedReader(new InputStreamReader(zipInputStream));
String contentStr;
while ((contentStr = in.readLine()) != null) {
//Process the contents
System.out.println(contentStr);
}
} catch (IOException e) {
//Handle the exception
}
我都看过 AWS S3 Java SDK - Download file help and Working with Zip and GZip files in Java。
虽然它们分别提供了从 S3 和 GZipped 文件下载和处理文件的方法,但它们无助于处理位于 S3 中的 GZipped 文件。我该怎么做?
目前我有:
try {
AmazonS3 s3Client = new AmazonS3Client(
new ProfileCredentialsProvider());
String URL = downloadURL.getPrimitiveJavaObject(arg0[0].get());
S3Object fileObj = s3Client.getObject(getBucket(URL), getFile(URL));
BufferedReader fileIn = new BufferedReader(new InputStreamReader(
fileObj.getObjectContent()));
String fileContent = "";
String line = fileIn.readLine();
while (line != null){
fileContent += line + "\n";
line = fileIn.readLine();
}
fileObj.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return "ERROR IOEXCEPTION";
}
显然,我没有处理文件的压缩性质,我的输出是:
����sU�3204�50�5010�20�24��L,(���O�V�M-.NLOU�R�U�����<s��<#�^�.wߐX�%w���������}C=�%�J3��.�����둚�S�ᜑ���ZQ�T�e��#sr�cdN#瘐:&�
S�BǔJ����P�<��
但是我无法实现上面给出的second question中的示例,因为文件不在本地,需要从S3下载。
我该怎么办?
我使用 Scanner
而不是 InputStream
解决了这个问题。
扫描器获取 GZIPInputStream 并逐行读取解压缩的文件:
fileObj = s3Client.getObject(new GetObjectRequest(oSummary.getBucketName(), oSummary.getKey()));
fileIn = new Scanner(new GZIPInputStream(fileObj.getObjectContent()));
我并不是在寻找这个问题,但我确实想通过实际解释为什么已经提供的解决方案有效来提高这个线程的质量。
不,这不是因为建议的扫描仪。这是因为通过将 fileObj.getObjectContent()
包装在解压缩内容的 GZIPInputStream
中,流被 解压 。
删除 scanner
但保留 GZIPInputStream
,一切仍然有效。
您必须使用 GZIPInputStream
来读取 GZIP 文件
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.build();
String URL = downloadURL.getPrimitiveJavaObject(arg0[0].get());
S3Object fileObj = s3Client.getObject(getBucket(URL), getFile(URL));
byte[] buffer = new byte[1024];
int n;
FileOutputStream fileOuputStream = new FileOutputStream("temp.gz");
BufferedInputStream bufferedInputStream = new BufferedInputStream( new GZIPInputStream(fileObj.getObjectContent()));
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOuputStream);
while ((n = bufferedInputStream.read(buffer)) != -1) {
gzipOutputStream.write(buffer);
}
gzipOutputStream.flush();
gzipOutputStream.close();
请尝试通过这种方式从 S3 下载 GZip 文件。
试试这个
BasicAWSCredentials creds = new BasicAWSCredentials("accessKey", "secretKey");
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds))
.withRegion(Regions).build();
String bucketName = "bucketName";
String keyName = "keyName";
S3Object fileObj = s3.getObject(new GetObjectRequest(bucketName, keyName));
Scanner fileIn = new Scanner(new GZIPInputStream(fileObj.getObjectContent()));
if (null != fileIn) {
while (fileIn.hasNext()) {
System.out.println("Line: " + fileIn.nextLine());
}
}
}
我正在努力使用 SDK 2.x 实现相同的目标。随着 SDK 2 中引入的新理念,我不得不在找到解决方案之前做一些研究。因此,为了使用 SDK 2.0 的人们的利益,在这里添加一个代码片段。
S3Client s3 = S3Client.builder()
.region(region)
.build();
//Using the key, get the object
GetObjectRequest request = GetObjectRequest.builder().bucket(bucketName).key(key).build();
//Read the object as input stream
InputStream inputStream = s3.getObject(request, ResponseTransformer.toBytes()).asInputStream();
final GZIPInputStream zipInputStream;
try {
//Convert it to GZIP stream
zipInputStream = new GZIPInputStream(inputStream);;
BufferedReader in = new BufferedReader(new InputStreamReader(zipInputStream));
String contentStr;
while ((contentStr = in.readLine()) != null) {
//Process the contents
System.out.println(contentStr);
}
} catch (IOException e) {
//Handle the exception
}