当我在eclipse上运行我的代码时,它运行时没有错误,但是当我在AWS Lambda上运行我的代码时,错误来自行文件.createNewFile(),当我检查我发现file.mkdir()是不工作即。目录不存在。代码是: -
public Resource get(String id) throws StorageException, StorageFileNotFoundException {
Resource resource = null;
File file = null;
try {
FileDetails fileDetails = fileStoreDAO.get(id);
System.out.println(fileDetails.getName());
if(fileDetails != null) {
String tempDir = storageProperties.getLocation();
file = new File(tempDir + File.separator + fileDetails.getName());
file.mkdir();
if(file.exists()) {
//p1: delete any file if existent in the directory;
file.delete();
}
file.createNewFile();
FileCopyUtils.copy(fileDetails.getFileBytes(), file);
// System.out.println("filecopyutil ke agli line me aa gya");
resource = new UrlResource(Paths.get(tempDir).resolve(fileDetails.getName()).toUri());
System.out.println("last me");
} else {
throw new StorageFileNotFoundException("No document found with id: " + id);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
if (e instanceof StorageFileNotFoundException) {
throw (StorageFileNotFoundException) e;
} else {
throw new StorageException("", e);
}
}
return resource;
}
,存储属性文件为: -
@Component
@ConfigurationProperties("storage")
public class StorageProperties {
private String location = "tmp";
private int maxDocSizeInBytes = 4194304;
/**
* Temporary Folder location for storing files
*/
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
在本地运行正常,但在AWS Lambda中运行时出错。
答案 0 :(得分:0)
使用Lambda时,您的代码在/var/task
内的只读文件系统中运行。
如果您需要保存临时文件,则应将其写入/tmp
分区。