【问题标题】:Write and then read files from /tmp directory in aws lambda using java使用 java 从 aws lambda 中的 /tmp 目录写入然后读取文件
【发布时间】:2023-03-29 10:49:01
【问题描述】:

我已经编写了 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 位置写入和读取文件。任何帮助将不胜感激。

【问题讨论】:

    标签: java amazon-web-services amazon-s3 aws-lambda


    【解决方案1】:

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

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

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

    【讨论】:

    • 您好,感谢您的及时回复。我已根据需要编辑了代码。另外,我得到了你指出的同一行的 fileNotFoundException,但我在参数中提供了文件名。即使这样我也得到了错误。
    • 基本上问题是我无法在 /tmp 文件夹中写入文件。
    • 我建议先做一些非常基本的调试。尝试打印/记录"/tmp/"+fileNameForDirUpload 的值并验证它看起来是否正确。我认为您可能在组合字符串中有太多斜杠。
    • "/tmp/"+fileNameForDirUpload 的打印值绝对正确。仍然无法在 /tmp 中写入任何文件。我再次编辑了代码以简化事情。如果您可以查看并找出问题所在。我们将不胜感激。
    • 我发现AWS mount tmp,分配1M给它。亚马逊如何获得最高评价超出了我的理解,但我似乎被平地社会所包围。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2018-07-12
    • 2018-12-15
    • 1970-01-01
    • 2019-08-18
    • 2018-01-04
    相关资源
    最近更新 更多