Ruby : 上传的文件在 heroku 中突然在数小时内找不到

Ruby : uploaded files suddenly become not found within hours in heroku

我有以下 post 代码供 Ruby Sinatra 上传图像文件:

post "/upload" do 
  File.open("public/uploads/" + params["image"][:filename], "wb") do |f|
    f.write(params["image"][:tempfile].read)
  end
end

以及以下 Java 代码将图像文件上传到示例。com/upload :

private static String boundary;
private static final String LINE_FEED = "\r\n";
private static HttpURLConnection httpConn;
private static OutputStream outputStream;
private static PrintWriter writer;

public static void upload(String requestURL, String fieldName, File
uploadFile) throws IOException {

    boundary = "===" + System.currentTimeMillis() + "===";

    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    outputStream = httpConn.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);

    String fileName = uploadFile.getName();
    writer.append("--" + boundary).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE_FEED);
    writer.append("Content-Type: "+ URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
    writer.append(LINE_FEED).flush();

    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();

    writer.append("--" + boundary + "--").append(LINE_FEED);
    writer.close();
    httpConn.getInputStream();
    httpConn.disconnect();
}

File uploadFile = new File("C:/myimage.png");
upload("http://www.example.com/upload", "image", uploadFile);

我的网站托管在 heroku 上。

调用upload()后,图片文件上传成功,示例中可以访问com/upload/myimage.png

但问题是:几个小时后,当我检查 url 以查看 myimage.png 时,我收到 "Not Found" 错误(heroku 日志中的 404 错误)

有什么想法吗?

抱歉我的英语不好:|

您不应将文件存储到 heroku 的本地文件系统。来自他们的 docs:

Ephemeral filesystem

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

建议将文件上传到 AWS S3 或其他云存储系统,而不是在本地存储文件。