title: unzip a file into /tmp folder using lambda function from s3 bucket
body: <p>I have a bucket by the name &quot;testing_lambda&quot;.  Also within this &quot;temp_lambda&quot; folder, it stored a zip file named &quot;test.zip&quot;.</p>
  <p>I wish to use lambda function to first read the file from that bucket folder and then unzip it to a /tmp folder within the lambda function before continue other process but I just cant make it work.</p>
  <p>I cant even try the simplest code by accessing the resource. I am wondering whether is there anyone can assist me.</p>
  <p>Code:</p>
  <pre><code>import boto3
  
  s3_resource = boto3.resource('s3')
  
  
  def lambda_handler(event, context):
      
      return {
          'statusCode': 200,
          'body': s3_resource.buckets.all()
      }
  </code></pre>
  <p>Error:</p>
  <pre><code>  &quot;errorMessage&quot;: &quot;Unable to marshal response: s3.bucketsCollection(s3.ServiceResource(), s3.Bucket) is not JSON serializable&quot;,
  </code></pre>
  <p>Edited:
  unzip-requirement.py</p>
  <pre><code>import os
  import shutil
  import sys
  import zipfile
  import boto3
  import io
  
  pkgdir = '/tmp/sls-py-req'
  
  sys.path.append(pkgdir)
  
  s3_resource = boto3.resource(&quot;s3&quot;)
  
  bucket = &quot;testing_lambda&quot;
  key = &quot;.test.zip&quot;
  response = s3_resource.Bucket(bucket)
  obj = response.Object(key)
  
  with io.BytesIO(obj.get()[&quot;Body&quot;].read()) as tf:
      if not os.path.exists(pkgdir):
          tempdir = '/tmp/_temp-sls-py-req'
          if os.path.exists(tempdir):
              shutil.rmtree(tempdir)
      
          default_layer_root = '/opt'
          lambda_root = os.getcwd() if os.environ.get('IS_LOCAL') == 'true' else default_layer_root
          zip_requirements = os.path.join(lambda_root, key)
      
          zipfile.ZipFile(zip_requirements, 'r').extractall(tempdir)
          os.rename(tempdir, pkgdir)  # Atomic
  </code></pre>
  <p>lambda_function.py
  This python is use to test whether I could import torch.</p>
  <pre><code>try:
      import unzip_requirements
  except ImportError:
      pass
  import json
  import torch
  
  def lambda_handler(event, context):
      d = {&quot;torch&quot;: torch.__version__}
      return json.dumps(d)
  </code></pre>
  <p>Current error:</p>
  <pre><code>START RequestId: e15cdba6-6167-448b-adb0-b52379ab563a Version: $LATEST
  [ERROR] NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
  Traceback (most recent call last):
    File &quot;/var/lang/lib/python3.7/imp.py&quot;, line 234, in load_module
      return load_source(name, filename, file)
    File &quot;/var/lang/lib/python3.7/imp.py&quot;, line 171, in load_source
      module = _load(spec)
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 696, in _load
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 677, in _load_unlocked
    File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 728, in exec_module
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed
    File &quot;/var/task/lambda_function.py&quot;, line 2, in &lt;module&gt;
      import unzip_requirements
    File &quot;/var/task/unzip_requirements.py&quot;, line 19, in &lt;module&gt;
      with io.BytesIO(obj.get()[&quot;Body&quot;].read()) as tf:
    File &quot;/var/runtime/boto3/resources/factory.py&quot;, line 520, in do_action
      response = action(self, *args, **kwargs)
    File &quot;/var/runtime/boto3/resources/action.py&quot;, line 83, in __call__
      response = getattr(parent.meta.client, operation_name)(*args, **params)
    File &quot;/var/runtime/botocore/client.py&quot;, line 357, in _api_call
      return self._make_api_call(operation_name, kwargs)
    File &quot;/var/runtime/botocore/client.py&quot;, line 676, in _make_api_call
      raise error_class(parsed_response, operation_name)
  END RequestId: e15cdba6-6167-448b-adb0-b52379ab563a
  REPORT RequestId: e15cdba6-6167-448b-adb0-b52379ab563a  Duration: 1478.68 ms    Billed Duration: 1479 ms    Memory Size: 516 MB Max Memory Used: 36 MB  
  Unknown application error occurred
  </code></pre>