title: how to unzip a zipped file in s3
body: <p>I have a zipped file in a folder in a s3 bucket. I want to unzip the file using boto3. This is my code so far.</p>
  <pre><code>def unzip_file(path, file_name):
      s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
      my_bucket = s3.Bucket(BUCKET)
  
      lst = list(my_bucket.objects.filter(Prefix=path))
      unzip_path = '/'.join(str(lst[0].key).split('/')[:-1])
  
      with zipfile.ZipFile(f&quot;{path}/{file_name}&quot;, 'r') as zip_ref:
          zip_ref.extractall(unzip_path)
  </code></pre>
  <p>But this just gives an error as follows</p>
  <pre><code>Traceback (most recent call last):
    File &quot;download.py&quot;, line 153, in &lt;module&gt;
      unzip_file(path, file_name)
    File &quot;download.py&quot;, line 32, in unzip_file
      with zipfile.ZipFile(f&quot;{path}/{file_name}&quot;, 'r') as zip_ref:
    File &quot;/Users/sashaanksekar/anaconda3/lib/python3.8/zipfile.py&quot;, line 1250, in __init__
      self.fp = io.open(file, filemode)
  FileNotFoundError: [Errno 2] No such file or directory: 'test_parent/test_num/test.zip'
  </code></pre>
  <p>How do I unzip the file with python and boto3?</p>
  <h1>[EDIT1]</h1>
  <p>I have edited the code such that the zipped file is now in memory. How do I extract all the files into S3.</p>
  <p>this is my code now</p>
  <pre><code>def unzip_file(r, path, file_name):
      s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
      my_bucket = s3.Bucket(BUCKET)
  
      if r.status_code == 200:
          filebytes = BytesIO(r.content)
          file = zipfile.ZipFile(filebytes)
  
          extract_folder = f&quot;{path}extract_test/&quot;
          
          # extract each file in file.namelist() and save in extract_folder here
  
  </code></pre>