Heroku - How to Write into "Tmp" Directory

Store file in directory tmp on heroku Rails

Heroku uses what is called an ephemeral filesystem. This means that your local filesystem is only accessible to a single dyno, and once the dyno is stopped, restarted, or moved, all files on the local filesystem are destroyed.

The only way for your Delayed Job process to transfer files to an outside process would be to store the files in a more permanent location. This could be S3, a database, Rackspace Files, etc, but for Heroku it cannot be the local filesystem.

However, if you are just looking to store the file in a temporary scratch location, it is fine to use the local filesystem. It looks like you may be having issues because the /app/tmp directory may not exist.

Try adding this to your worker:

Dir.mkdir(Rails.root.join('tmp'))

Or, add the tmp directory to your git repository.

mkdir tmp
touch tmp/.keep
git add tmp/.keep
git commit -m "Add tmp directory to app repository."

Downloading from /tmp folder in Heroku

The /tmp directory on Heroku is exactly that – temporary. Even if you store the file in the /tmp file, it won't be persisted for long enough that any users will be able to access it. Instead, you should look into an integrated storage solution like Amazon AWS.

With that in place, your users should be able to access those CSV files directly from your storage host without needing to tie up any Heroku dynos/resources.

Heroku /tmp folder deletion

heroku never explicitely removes files from your tmp folder.

However it is not shared between instances of your application (your dynos).

This means you can assume the tmp folder to be emptied every time you deploy your application.

As you should always be able to deploy, you need, for your own sake, to architect your app with that in mind and not rely on the tmp folder to keep files longer than the user's HTTP request.

How to create temp directory in heroku/docker using Java?

 File tempDirectory = new File(new File(System.getProperty("java.io.tmpdir")), "files");
if(tempDirectory.exists()){
System.out.println("something");
}else{
tempDirectory.mkdirs();
}

File file = new File(tempDirectory.getAbsolutePath()+"/abcd.txt");
if(!file.exists()){
file.createNewFile();
}
String file2= new File(tempDirectory.getAbsolutePath()+"/something.txt").getAbsolutePath();

System.out.println(file2);

Works totally fine at my end. The only problem you might be having is

String filePath = new File(tempDirectory.getAbsolutePath() + "/temp.exe").getAbsolutePath();

This doesn't create the file in the temp directory you have created. It just returns the absolute path if it was to be saved in the directory mentioned. This might be the reason you are getting not found error. Try actually saving by using

file.transferTo(wherefileneedstobesavedlocation);

accessing tmp files Heroku Flask App and/or utilising AWS S3 storage

This is actually two separated questions but I guess that I can help on both:

werkzeug.routing.BuildError: Could not build url for endpoint 'app' with values ['filename']. Did you mean 'add' instead?

For this error, you need to understand that the function app doesn't exist. So Flask doesn't know what to do. Instead refer to the doc and return that:

return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

For the S3 Error, you have to know that the file object behaves like an old audio tape: once read, you need to rewind it to the beginning (some doc here).

I guess that this code will do the trick:

s3 = boto.connect_s3()
bucket = s3.get_bucket("facesappstorage")
key = bucket.new_key(filename)
# Have a look here
file.seek(0)
key.set_contents_from_file(file, headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None)


Related Topics



Leave a reply



Submit