首页 > 解决方案 > 使用 java 从 aws lambda 中的 /tmp 目录写入然后读取文件

问题描述

我已经编写了 AWS Lambda 代码,我需要将图像存储在 aws lambda 的 /tmp 位置。下面是我的代码:

String fileLocation = "loc1/loc2/";
String imageNameWithoutExt = "image1";
//creating directories first below storing the image
boolean status = new File("/tmp/"+fileLocation).mkdirs();
if(status == true){
File targetFile = File.createTempFile(imageNameWithoutExt,".jpg",new File("/tmp/"+fileLocation));
    FileOutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    outStream.close();
}else{
    System.out.println("unable to create directory inside /tmp/");
}

作为回应,它正在打印 else 语句:

unable to create directory inside /tmp/

我需要进行什么修改才能从 /tmp 位置写入和读取文件。任何帮助,将不胜感激。

标签: javaamazon-web-servicesamazon-s3aws-lambda

解决方案


在这行代码中,您没有设置文件名:

//write file in /tmp folder of aws Lambda
File targetFile = new File("/tmp/");

我想也许您没有显示所有代码,因为我看不到image1.jpg错误消息中的字符串来自哪里,但是需要将该文件名添加到您传递File构造函数的参数中。


推荐阅读