title: accessing tmp files Heroku Flask App and/or utilising AWS S3 storage
body: <p>I am trying to access files uploaded to Heroku's ephemeral file storage system.(I understand that these files will only be available for that specific dyno)  The documentation says that the /tmp directory is the location to upload to.</p>
  <p>I believe my endpoint for the redirect url is incorrect:</p>
  <p>.py</p>
  <pre><code>UPLOAD_FOLDER_FACES = '/app/tmp/'
  app.config['UPLOAD_FOLDER_FACES'] = UPLOAD_FOLDER_FACES
  </code></pre>
  <pre><code>@app.route(&quot;/AddFace&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
  def add_face():
      if 'image' not in request.files:
          flash('No file part')
          return redirect(request.url)
      file = request.files['image']
      if file.filename == '':
          flash('No image selected for uploading')
          return redirect(request.url)
      if file and allowed_file(file.filename):
          filename = secure_filename(file.filename)
          file.save(os.path.join(app.config['UPLOAD_FOLDER_FACES'], filename))        
          flash('Face successfully uploaded, saved and displayed')
          return render_template('addface.html', filename=filename)
      else:
          flash('Allowed image types are -&gt; png, jpg, jpeg, gif')
          return redirect(request.url)
      return render_template(&quot;addface.html&quot;)
  </code></pre>
  <pre><code>@app.route('/displayfaces/&lt;filename&gt;')
  def display_image_face(filename):
      # print('display_image filename: ' + filename)
      return redirect(url_for('app', filename='tmp/' + filename), code=301)  &lt;-----error
  </code></pre>
  <p>HTML</p>
  <pre><code>{% if filename %}
      &lt;div&gt;
          &lt;img src=&quot;{{ url_for('display_image_face', filename=filename) }}&quot;&gt;
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  <p>I have explored the file structure on heroku using bash:</p>
  <pre><code>C:\Flask\app&gt;heroku run bash --app faces-application
  Running bash on ⬢ faces-application... up, run.3918 (Free)
  ~ $ ls
  Aptfile  forms.py  gui.py  Procfile  requirements.txt  runtime.txt  static  templates
  ~ $ pwd
  /app
  ~ $ cd ..
  / $ ls
  app  bin  dev  etc  lib  lib64  lost+found  proc  sbin  sys  tmp  usr  var
  / $ pwd
  /
  / $ cd var
  /var $ ls
  lib  log  tmp
  /var $ pwd
  /var
  </code></pre>
  <p>Following this I discovered there was no /tmp in the app/ directory so I inserted the code into the homepage to mkdir if not exists.</p>
  <pre><code>@app.route(&quot;/&quot;)
  def home():
      if not os.path.exists('/app/tmp/'):
          directory = &quot;tmp&quot;
          parent_directory = &quot;/app/&quot;
          path = os.path.join(parent_directory, directory)
          os.mkdir(path)
      return render_template(&quot;index.html&quot;)
  </code></pre>
  <p>This also did not yield a successful result.</p>
  <p><strong>Which /tmp are the Heroku uploads associated with?</strong>  I'm wondering do I need to create a tmp directory within /app/ because Heroku isn't creating it?  or  Does Heroku use the '/' tmp directory or the /var/ tmp directory?</p>
  <p>When I attempt to upload I receive this error regarding endpoint for url which I am using to display the uploaded image.</p>
  <pre><code>2020-08-29T23:25:24.584621+00:00 app[web.1]: 10.15.190.6 - - [29/Aug/2020:23:25:24 +0000] &quot;POST /AddFace HTTP/1.1&quot; 200 3204 &quot;https://faces-application.herokuapp.com/AddFace&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36&quot;
  2020-08-29T23:25:24.738699+00:00 app[web.1]: [2020-08-29 23:25:24,734] ERROR in app: Exception on /displayfaces/Jason_Smiles.jpg [GET]
  2020-08-29T23:25:24.738700+00:00 app[web.1]: Traceback (most recent call last):
  2020-08-29T23:25:24.738701+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 2447, in wsgi_app
  2020-08-29T23:25:24.738701+00:00 app[web.1]: response = self.full_dispatch_request()
  2020-08-29T23:25:24.738702+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1952, in full_dispatch_request
  2020-08-29T23:25:24.738702+00:00 app[web.1]: rv = self.handle_user_exception(e)
  2020-08-29T23:25:24.738703+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1821, in handle_user_exception
  2020-08-29T23:25:24.738703+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
  2020-08-29T23:25:24.738704+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py&quot;, line 39, in reraise
  2020-08-29T23:25:24.738704+00:00 app[web.1]: raise value
  2020-08-29T23:25:24.738705+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1950, in full_dispatch_request
  2020-08-29T23:25:24.738705+00:00 app[web.1]: rv = self.dispatch_request()
  2020-08-29T23:25:24.738706+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1936, in dispatch_request
  2020-08-29T23:25:24.738708+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
  2020-08-29T23:25:24.738709+00:00 app[web.1]: File &quot;/app/gui.py&quot;, line 556, in display_image_face
  2020-08-29T23:25:24.738709+00:00 app[web.1]: return redirect(url_for('app', filename='tmp/' + filename), code=301)
  2020-08-29T23:25:24.738710+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/helpers.py&quot;, line 370, in url_for
  2020-08-29T23:25:24.738710+00:00 app[web.1]: return appctx.app.handle_url_build_error(error, endpoint, values)
  2020-08-29T23:25:24.738735+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 2216, in handle_url_build_error
  2020-08-29T23:25:24.738736+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
  2020-08-29T23:25:24.738737+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py&quot;, line 39, in reraise
  2020-08-29T23:25:24.738737+00:00 app[web.1]: raise value
  2020-08-29T23:25:24.738738+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/helpers.py&quot;, line 358, in url_for
  2020-08-29T23:25:24.738738+00:00 app[web.1]: endpoint, values, method=method, force_external=external
  2020-08-29T23:25:24.738738+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/werkzeug/routing.py&quot;, line 2179, in build
  2020-08-29T23:25:24.738739+00:00 app[web.1]: raise BuildError(endpoint, values, method, self)
  2020-08-29T23:25:24.738745+00:00 app[web.1]: werkzeug.routing.BuildError: Could not build url for endpoint 'app' with values ['filename']. Did you mean 'add' instead?
  2020-08-29T23:25:24.739542+00:00 app[web.1]: 10.15.190.6 - - [29/Aug/2020:23:25:24 +0000] &quot;GET /displayfaces/Jason_Smiles.jpg HTTP/1.1&quot; 500 290 &quot;https://faces-application.herokuapp.com/AddFace&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36&quot;
  2020-08-29T23:25:24.739119+00:00 heroku[router]: at=info method=GET path=&quot;/displayfaces/Jason_Smiles.jpg&quot; host=faces-application.herokuapp.com request_id=c03a89d5-b95c-4803-b9aa-f12567c1f127 fwd=&quot;*********&quot; dyno=web.1 connect=0ms service=7ms status=500 bytes=470 protocol=https
  </code></pre>
  <p><strong>To recap:</strong>
  I have tried using '/' , 'var/' , 'app/' for the mount point without any success.  If anyone can shed some light on how to upload and access the ephemeral storage (I have read the docs and browsed extensively for a working example) I would be extremely grateful.  I am slowly moving towards an s3 storage solution but the ephemeral system was ideal for my use case.  I am providing users with a demo app to assess the viability of the application and the idea that any images they upload will be erased after their session terminates is perfect. I know there must be someone out there that has successfully used Heroku's /tmp.....</p>
  <p><em><strong>If all else fails and s3 is the solution:</strong></em></p>
  <p>If you can identify any issues with the s3 code in the add_face() that would be also a considerable help.</p>
  <pre><code>s3 = boto.connect_s3()
  bucket = s3.get_bucket(&quot;facesappstorage&quot;)
  key = bucket.new_key(filename)
  key.set_contents_from_file(file, headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None)
  </code></pre>
  <p>Log error:</p>
  <pre><code>2020-08-29T23:20:11.674479+00:00 app[web.1]: [2020-08-29 23:20:11,670] ERROR in app: Exception on /AddFace [POST]
  2020-08-29T23:20:11.674489+00:00 app[web.1]: Traceback (most recent call last):
  2020-08-29T23:20:11.674490+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 2447, in wsgi_app
  2020-08-29T23:20:11.674490+00:00 app[web.1]: response = self.full_dispatch_request()
  2020-08-29T23:20:11.674506+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1952, in full_dispatch_request
  2020-08-29T23:20:11.674507+00:00 app[web.1]: rv = self.handle_user_exception(e)
  2020-08-29T23:20:11.674508+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1821, in handle_user_exception
  2020-08-29T23:20:11.674508+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
  2020-08-29T23:20:11.674508+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py&quot;, line 39, in reraise
  2020-08-29T23:20:11.674509+00:00 app[web.1]: raise value
  2020-08-29T23:20:11.674510+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1950, in full_dispatch_request
  2020-08-29T23:20:11.674510+00:00 app[web.1]: rv = self.dispatch_request()
  2020-08-29T23:20:11.674510+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/flask/app.py&quot;, line 1936, in dispatch_request
  2020-08-29T23:20:11.674511+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
  2020-08-29T23:20:11.674511+00:00 app[web.1]: File &quot;/app/gui.py&quot;, line 150, in add_face
  2020-08-29T23:20:11.674512+00:00 app[web.1]: key.set_contents_from_file(file, headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None)
  2020-08-29T23:20:11.674512+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.6/site-packages/boto/s3/key.py&quot;, line 1249, in set_contents_from_file
  2020-08-29T23:20:11.674512+00:00 app[web.1]: raise AttributeError('fp is at EOF. Use rewind option '
  2020-08-29T23:20:11.674519+00:00 app[web.1]: AttributeError: fp is at EOF. Use rewind option or seek() to data start.
  2020-08-29T23:20:11.676076+00:00 app[web.1]: 10.9.6.145 - - [29/Aug/2020:23:20:11 +0000] &quot;POST /AddFace HTTP/1.1&quot; 500 290 &quot;https://faces-application.herokuapp.com/AddFace&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36&quot;
  </code></pre>
  <p><strong>SOLUTION</strong></p>
  <pre><code>UPLOAD_FOLDER_FACES = '/tmp/'
  
  @app.route('/displayfaces/&lt;filename&gt;')
  def display_image_face(filename):
      # print('display_image filename: ' + filename)
      return send_from_directory(app.config['UPLOAD_FOLDER_FACES'], filename)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/GLK39.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GLK39.png" alt="Display Image from /tmp Working!!!" /></a></p>
  <p><a href="https://i.stack.imgur.com/VEdCF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VEdCF.png" alt="No More HTML Errors..." /></a></p>