title: Uploading file to AWS S3 through Chalice API call
body: <p>I'm trying to upload a file to my S3 bucket through Chalice (I'm playing around with it currently, still new to this). However, I can't seem to get it right.</p>
  
  <p>I have AWS setup correctly, doing the tutorial successfully returns me some messages. Then I try to do some upload/download, and problem shows up.</p>
  
  <pre><code>s3 = boto3.resource('s3', region_name=&lt;some region name, in this case oregon&gt;)
  BUCKET= 'mybucket'
  UPLOAD_FOLDER = os.path.abspath('')  # the file I wanna upload is in the same folder as my app.py, so I simply get the current folder name
  
  @app.route('/upload/{file_name}', methods=['PUT'])
  def upload_to_s3(file_name):
      s3.meta.client.upload_file(UPLOAD_FOLDER+file_name, BUCKET, file_name)
      return Response(message='upload successful',
                      status_code=200,
                      headers={'Content-Type': 'text/plain'}
      )
  </code></pre>
  
  <p>Please don't worry about how I set my file path, unless that's the issue, of course.</p>
  
  <p>I got the error log:</p>
  
  <blockquote>
    <p>No such file or directory: ''</p>
  </blockquote>
  
  <p>in this case <code>file_name</code> is just <code>mypic.jpg</code>.</p>
  
  <p>I'm wondering why the <code>UPLOAD_FOLDER</code> part is not being picked up. Also, for the reference, it seems like using absolute path will be troublesome with Chalice (while testing, I've seen the code being moved to <code>/var/task/</code>)</p>
  
  <p>Does anyone know how to set it up correctly?</p>
  
  <p>EDIT:</p>
  
  <p>the complete script</p>
  
  <pre><code>from chalice import Chalice, Response
  
  import boto3
  
  app = Chalice(app_name='helloworld')  # I'm just modifying the script I used for the tutorial 
  s3 = boto3.client('s3', region_name='us-west-2')
  BUCKET = 'chalicetest1'
  
  @app.route('/')
  def index():
      return {'status_code': 200,
              'message': 'welcome to test API'}
  
  @app.route('/upload/{file_name}, methods=['PUT'], content_types=['application/octet-stream'])
  def upload_to_s3(file_name):
      try:
          body = app.current_request.raw_body
          temp_file = '/tmp/' + file_name
          with open(temp_file, 'wb') as f:
              f.write(body)
          s3.upload_file(temp_file, BUCKET, file_name)
          return Response(message='upload successful',
                          headers=['Content-Type': 'text/plain'],
                          status_code=200)
      except Exception, e:
          app.log.error('error occurred during upload %s' % e)
          return Response(message='upload failed',
                          headers=['Content-Type': 'text/plain'],
                          status_code=400)
  </code></pre>