title: NoSuchKey in resize images using python on AWS (lambda + s3)
body: <p>I am trying to resize images as soon as they are uploaded to s3.</p>
  <p>I want that as soon as there is a PUT, a copy of it is made with the name <code>300x300_same_image_name.jpg</code> in the same bucket.</p>
  <p>For that I created a lambda aws function and I was copying and updating some codes that I found from tutorials.</p>
  <p>My code:</p>
  <pre class="lang-py prettyprint-override"><code>import boto3
  import PIL
  from PIL import Image
  import uuid
  from io import BytesIO
  import os
  
  
  def lambda_handler(event, context):
      key = event[&quot;Records&quot;][0][&quot;s3&quot;][&quot;object&quot;][&quot;key&quot;]
      size = event[&quot;Records&quot;][0][&quot;s3&quot;][&quot;object&quot;][&quot;size&quot;]
      result_url = resize_image(os.environ[&quot;BUCKET&quot;], key, size)
      response = {
          &quot;statusCode&quot;: 301,
          &quot;body&quot;: &quot;&quot;,
          &quot;headers&quot;: {
              &quot;location&quot;: result_url
          }
  
      }
      return response
  
  
  def resize_image(bucket_name, key, size):
      size_str = str(size)
      size_split = size_str.split('x')
      s3 = boto3.resource('s3')
      obj = s3.Object(
          bucket_name=bucket_name,
          key=key,
      )
      obj_body = obj.get()['Body'].read()
      img = Image.open(BytesIO(obj_body))
      img = img.resize((int(size_split[0]), int(size_split[1])), PIL.Image.ANTIALIAS)
      buffer = BytesIO()
      img.save(buffer, 'JPEG')
      buffer.seek(0)
      resized_key = &quot;{size}_{key}&quot;.format(size=size, key=key)
      obj = s3.Object(
          bucket_name=bucket_name,
          key=resized_key,
      )
      obj.put(Body=buffer, ContentType='image/jpeg')
      return resized_image_url(resized_key, bucket_name, os.environ[&quot;AWS_REGION&quot;])
  
  
  def resized_image_url(resized_key, bucket, region):
      return &quot;https://s3-{region}.amazonaws.com/{bucket}/{resized_key}&quot;.format(bucket=bucket, region=region, resized_key=resized_key)
  </code></pre>
  <p>My problem: running the example (in aws function creation) I got this error:</p>
  <pre><code>Output: 
  {
    &quot;errorMessage&quot;: &quot;An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.&quot;,
    &quot;errorType&quot;: &quot;NoSuchKey&quot;,
    &quot;stackTrace&quot;: [
      &quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 15, in lambda_handler\n    result_url = resize_image(os.environ[\&quot;BUCKET\&quot;], key, size)\n&quot;,
      &quot;  File \&quot;/var/task/lambda_function.py\&quot;, line 35, in resize_image\n    obj_body = obj.get()['Body'].read()\n&quot;,
      &quot;  File \&quot;/var/runtime/boto3/resources/factory.py\&quot;, line 520, in do_action\n    response = action(self, *args, **kwargs)\n&quot;,
      &quot;  File \&quot;/var/runtime/boto3/resources/action.py\&quot;, line 83, in __call__\n    response = getattr(parent.meta.client, operation_name)(*args, **params)\n&quot;,
      &quot;  File \&quot;/var/runtime/botocore/client.py\&quot;, line 316, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n&quot;,
      &quot;  File \&quot;/var/runtime/botocore/client.py\&quot;, line 635, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n&quot;
    ]
  }
  </code></pre>
  <p>Looking for the web my line <code>obj_body = obj.get()['Body'].read()</code> seems ok. Does anyone have any light?</p>
  <h3>Solution</h3>
  <p>I understood my mistake. Using AWS tests there was no object. However running the function I got it. Thank you.</p>