java - AWS Lambda: How to extract a tgz file in a S3 bucket and put it in another S3 bucket -
i have s3 bucket named "source". many '.tgz' files being pushed bucket in real-time. wrote java code extracting '.tgz' file , pushing "destination" bucket. pushed code lambda function. got '.tgz' file inputstream in java code. how extract in lambda ? i'm not able create file in lambda, throws "filenotfound(permission denied)" in java.
amazons3 s3client = new amazons3client(); s3object s3object = s3client.getobject(new getobjectrequest(srcbucket, srckey)); inputstream objectdata = s3object.getobjectcontent(); file file = new file(s3object.getkey()); outputstream writer = new bufferedoutputstream(new fileoutputstream(file)); <--- throws filenotfound(permission denied) here
don't use file
or fileoutputstream
, use s3client.putobject()
. read tgz file, can use apache commons compress. example:
archiveinputstream tar = new archiveinputstreamfactory(). createarchiveinputstream("tar", new gzipinputstream(objectdata)); archiveentry entry; while ((entry = tar.getnextentry()) != null) { if (!entry.isdirectory()) { byte[] objectbytes = new byte[entry.getsize()]; tar.read(objectbytes); objectmetadata metadata = new objectmetadata(); metadata.setcontentlength(objectbytes.length); metadata.setcontenttype("application/octet-stream"); s3client.putobject(destbucket, entry.getname(), new bytearrayinputstream(objectbytes), metadata); } }
Comments
Post a Comment