Aywac
Aywac

Reputation: 61

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

I have the following post code for Ruby Sinatra to upload image files :

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

And the following Java codes to upload the image file to example.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);

And my website is hosted at heroku .

After calling upload(), the image file is successfully uploaded, and i can access to it in example.com/upload/myimage.png

But the problem is : After some hours, when i check the url to see myimage.png, i got "Not Found" error ( 404 error in heroku logs )

Any ideas ?

Sorry for my bad english :|

Upvotes: 1

Views: 106

Answers (1)

spickermann
spickermann

Reputation: 107037

You should not store files to heroku's local file system. From their 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.

Instead of storing files locally it is recommended to upload files to AWS S3 or other cloud storage systems.

Upvotes: 4

Related Questions