Transferring files from AWS lambda tmp folder to sftp server

Rooky Coder :

I have created a AWS lambda function that takes some files from an S3 bucket, zips them and transfers the zipped file to a sftp server. When I look in the server, I see that the tmp folder has been carries over with the files and a tmp folder gets created inside the zip file. When I open the zip file, there is a tmp folder and inside that folder are the files that I had zipped. I have scoured the internet and AWS trying to figure out how to change the directory in AWS Lambda when I am retrieving the files to be zipped, but have not had any luck. I don't want to have a tmp folder in my zip file. When I unzip the zip file, I just want to see the files that I had selected to be zipped without any folders. Does anyone know how to do this? I am programming in Java.

My code is below.

    private DownloadFile(){
          File localFile = new File(fileName);
          //pull data and audit files from s3 bucket
          s3Client.getObject(new GetObjectRequest("pie-dd-demo/daniel20", fileName), localFile);
          zipOS = new ZipOutputStream(fos);

         //send files to be zipped
         writeToZipFile(fileName, zipOS);
     }




    public static void writeToZipFile(String path, ZipOutputStream zipStream)
        throws FileNotFoundException, IOException {

        File aFile = new File(path);
        FileInputStream fis = new FileInputStream(aFile);
        ZipEntry zipEntry = new ZipEntry(path);
        try {
            zipStream.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipStream.write(bytes, 0, length);
                System.out.println(path + "write to zipfile complete");
          }

    } catch (FileNotFoundException exception) {
        // Output expected FileNotFoundExceptions.

    } catch (Exception exception) {
        // Output unexpected Exceptions.

    }
    zipStream.closeEntry();
    fis.close();
  }
jarmod :

I think the problem is that you are creating a zip entry using new ZipEntry(path) and that means that the resulting zip file will contain the full path as the name of the zip entry.

You can retrieve the actual filename from a full path/file in Java as follows:

File f = new File("/tmp/folder/cat.png");
String fname = f.getName();

You can then use fname to create the zip entry by calling new ZipEntry(fname).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194702&siteId=1