title: Writing a file to S3 using Lambda in Python with AWS
body: <p>In AWS, I'm trying to save a file to S3 in Python using a Lambda function. While this works on my local computer, I am unable to get it to work in Lambda. I've been working on this problem for most of the day and would appreciate help. Thank you.</p>
  
  <pre><code>def pdfToTable(PDFfilename, apiKey, fileExt, bucket, key):
  
      # parsing a PDF using an API
      fileData = (PDFfilename, open(PDFfilename, "rb"))
      files = {"f": fileData}
      postUrl = "https://pdftables.com/api?key={0}&amp;format={1}".format(apiKey, fileExt)
      response = requests.post(postUrl, files=files)
      response.raise_for_status()
  
      # this code is probably the problem!
      s3 = boto3.resource('s3')
      bucket = s3.Bucket('transportation.manifests.parsed')
      with open('/tmp/output2.csv', 'rb') as data:
          data.write(response.content)
          key = 'csv/' + key
          bucket.upload_fileobj(data, key)
  </code></pre>
  
  <hr>
  
  <pre><code>    # FYI, on my own computer, this saves the file
      with open('output.csv', "wb") as f:
          f.write(response.content)
  </code></pre>
  
  <p>In S3, there is a bucket <code>transportation.manifests.parsed</code> containing the folder <code>csv</code> where the file should be saved. </p>
  
  <p>The type of <code>response.content</code> is bytes.</p>
  
  <p>From AWS, the error from the current set-up above is <code>[Errno 2] No such file or directory: '/tmp/output2.csv': FileNotFoundError.</code> In fact, my goal is to save the file to the csv folder under a unique name, so <code>tmp/output2.csv</code> might not be the best approach. Any guidance?</p>
  
  <p>In addition, I've tried to use <code>wb</code> and <code>w</code> instead of <code>rb</code> also to no avail. The error with <code>wb</code> is <code>Input &lt;_io.BufferedWriter name='/tmp/output2.csv'&gt; of type: &lt;class '_io.BufferedWriter'&gt; is not supported.</code> The <a href="https://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.upload_fileobj" rel="nofollow noreferrer">documentation</a> suggests that using 'rb' is the recommended usage, but I do not understand why that would be the case. </p>
  
  <p>Also, I've tried <code>s3_client.put_object(Key=key, Body=response.content, Bucket=bucket)</code> but receive <code>An error occurred (404) when calling the HeadObject operation: Not Found</code>.</p>