简体   繁体   中英

Download and save files to a Heroku dyno filesystem

I have a Python program hosted on a Heroku dyno, in which I want to periodically download a file (an img from an external URL) and save it somewhere for the program to access.

Is this at all possible? And if so, how do I defined the path for my app to access those files while on Heroku?

You didn't mention what exactly you will do with the downloaded image, neither how long your application will need it. IMHO it's more future-proof to save those kinds of file in Amazon S3. Look at Heroku's documentation here to find out why. Pay attention to this line:

S3 allows you to offload the storage of static files from your app. This is crucial on Heroku, because your app's dynos have an ephemeral filesystem. This means that all files that aren't part of your application's slug are lost whenever a dyno restarts or is replaced (this happens at least once daily).

Basically, once you restart your application, the downloaded image will be gone. If your application needs the image for more than 24 hours, the image will be gone too because the dyno will have restarted ìn the meantime.

If however, after all the reasons mentioned above you still think AmazonS3 is not necessary, and whish to access the image directly from the dyno's filesystem, then look at this stackoverflow post here :

import os
os.path.abspath(os.getcwd())

You can then use the current working directory to construct the path where the images are saved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM