title: Is there a way to write chunks with python to aws s3
body: <p>The API I am building currently saves files from an endpoint to my EC2 instance. I want to save the files directly to an S3 but I am having trouble streaming the chunks to the S3. This is the original code:</p>
  
  <pre class="lang-py prettyprint-override"><code>    def _get_file(self, id, values, current, count):
          msg = 'Downloading ID: {} de {}'.format(id, current, count)
          log.info(msg)
  
          for i in range(TRY_COUNT):
              try:
                  r = self._session.get(values['url'], stream=True, timeout=TIMEOUT)
                  if r.status_code == 200:
                      with open(values['path_file'], 'wb') as f:
                          for chunk in r.iter_content(1024):
                              f.write(chunk)
                  if values['acuse']:
                      self._save_acuse(id, values['acuse'])
                  return
              except exceptions.Timeout:
                  log.debug('Timeout')
                  continue
              except Exception as e:
                  log.error(str(e))
                  return
          msg = 'Timeout: {}'.format(id)
          log.error(msg)
          return
  </code></pre>
  
  <p>The modified program is creating the directories and files in the S3 bucket but the actual txt files have 0 bytes, so it's not writing the chunks to the S3 properly.</p>
  
  <p>Is it even possible to stream the chunks to the S3? 
  Or do I have to store the files in the EC2 and then upload them to the S3 bucket?</p>