【发布时间】:2020-06-22 14:11:48
【问题描述】:
我创建了一个 AWS lambda 函数,它从 S3 存储桶中获取一些文件,压缩它们并将压缩文件传输到 sftp 服务器。当我查看服务器时,我看到 tmp 文件夹已与文件一起保留,并且在 zip 文件中创建了一个 tmp 文件夹。当我打开 zip 文件时,有一个 tmp 文件夹,该文件夹内是我压缩的文件。当我检索要压缩的文件时,我已经搜索了互联网和 AWS,试图弄清楚如何更改 AWS Lambda 中的目录,但没有任何运气。我不想在我的 zip 文件中有 tmp 文件夹。当我解压缩 zip 文件时,我只想查看我选择要压缩的文件,没有任何文件夹。有谁知道如何做到这一点?我正在用 Java 编程。
我的代码如下。
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();
}
【问题讨论】:
-
您的压缩包中的文件是直接在
tmp文件夹中还是在tmp/pie-dd-demo中?如果是后者,那么我们必须研究 ZipEntries 是如何构建的。否则,您将始终拥有文件夹。 -
不,
pie-dd-demo不在我的 tmp 文件夹中。那是我的 s3 存储桶中的文件路径。在我提取文件并将其压缩后,它会存储在函数调用之间的 AWS lambda 的 tmp 文件夹中。这就是我有问题的地方。我有从 Aws Lambda tmp 文件中获取文件并将其传输到远程服务器的函数。当它传输时,它会带上 tmp 文件夹(完整文件路径)。因此,在服务器中创建了一个包含 zip 文件tmp/zipFileName的 tmp 文件夹。我只想传输 zip 文件而不是文件夹。 -
@jarmod 我无法让
new ZipEntry(path.basename(pathname))工作。当我编写基本名称的方法并使用path.substring()定义基本名称时,我会遇到类型冲突,因为路径是文件类型,子字符串是字符串函数。我也试过FilenameUtils.getBaseName(path)。这删除了 tmp 文件夹,但也将文件更改为 unix excitable。 -
@jarmod - 这行得通。谢谢。
标签: java amazon-web-services aws-lambda