node.js 在 heroku 上一段时间后图像不断丢失

node.js on heroku keeps losing image after a while

我的 Node.js 应用程序在一段时间后不断丢失静态图像(.jpg、.png...)。它不会丢失我本地 win10 桌面上的任何图像,甚至在 heroku 上,我的 webpack bundle.js 是从相同的静态路由(/pub 或 /dist)提供的,它们工作得很好。不知何故只有静态图像,当我第一次上传时,它们在前几分钟可以正常使用,然后过了一会儿,它就消失了。我使用 express.static 进行静态路由声明,使用 multer 进行文件上传。用于测试的文件都是小写的.jpg(因为我听说heroku任意更改所有大写扩展名)所以我不知道是什么导致了问题。

服务器代码:

const storage = multer.diskStorage({
  destination: (req,file,cb)=>{
    cb(null,'pub/')
  },
  filename: (req,file,cb)=>{
    cb(null,Date.now() + file.originalname)
  }
})
const upload = multer({storage:storage})

//access to static files
app.use('/pub', express.static(pubDir))
app.use('/dist', express.static(dstDir))

app.post('/modwimg',upload.any(),(req,res,next)=>{
//here I connect filename from files array to db
})

然后如果有客户端请求,服务器从数据库中获取文件名并将“/pub/”放在它前面。它在我的本地机器和 heroku 上都工作得很好。只是heroku上的图片过一段时间就消失了

heroku 文件系统是临时的。如果您希望允许用户将文件上传到您的应用程序,您将需要使用外部存储,如 S3、数据库 blob 或托管服务,如 cloudinary。有关详细信息,请参阅此线程:https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/