title: Python Flask- how can I read the size of an image before uploading it to Amazon s3
body: <p>This question might be fairly straightforward, if you have some experience with <code>Python Flask</code>, <code>Boto3</code>, <code>Pillow</code> (a.k.a. <code>PIL</code>).</p>
  
  <p>I'm attempting to receive an incoming image from a client (only allowing <code>.jpg</code>, <code>.jpeg</code>, <code>.tif</code>,) and i'd like to read the dimensions of the image before uploading it to Amazon S3 using <code>Boto3</code>.</p>
  
  <p>The code is fairly straight forward:</p>
  
  <pre><code>file = request.files['file'] 
  # produces an instance of FileStorage
  
  asset = models.Asset(file, AssetType.profile_img, donor.id) 
  # a model managed by the ORM
  
  img = Image.open(BytesIO(file.stream.read()))
  # produces a PIL Image object
  
  size = img.size
  # read the size of the Image object
  
  asset.width = size[0]
  asset.height = size[1]
  # set the size to the ORM
  
  response = s3.Object('my-bucket', asset.s3_key()).put(Body=file)
  # upload to S3
  </code></pre>
  
  <p>Here's the catch, I can either (A) read the image OR (B) upload to s3, but I can't do both. Literally, commenting out one or the other produces the desired operation, but not both in combination.</p>
  
  <p>I've narrowed it down to the upload. It's my belief that somewhere along the line, the file.strea.read() operation is causing an issue with the Boto3 upload, but I can't figure it out. Can you?</p>
  
  <p>Thanks in advance.</p>