title: How to read the content from tar file in lambda function in AWS
body: <p>I want to read content which is inside of a .tar file without extracting the tar file. I tried my local machine and successfully, I am able to read the content of the file. Just, I passed the path of the .tar file. For doing this I used one library called <strong>tarfile</strong>. With the help of this library I did it. Now I am trying to do same in AWS using the lambda function but I am not able to do it. Can you suggest any alternative for that ?</p>
  <pre><code>import tarfile
  file_path = r'C:/Users/xyz/Downloads/abc.tar'
  tar = tarfile.open(file_path, &quot;r:gz&quot;)
  for member in tar.getnames():
      name = str(member) 
      if name.endswith(&quot;.txt&quot;):
          f = tar.extractfile(name)
          if f:
              for i in f:
                  print(str(i))
  </code></pre>
  <p>Code for the AWS</p>
  <pre><code>s3 = boto3.client('s3')
  Bucket='abc-logs'
  if event:
          for i in event:
              file_obj = event[&quot;Records&quot;][0]
              Key = str(file_obj['s3']['object']['key'])
              if file_name_split == 'PERFORMANCE':
                  print(&quot;Key&quot;,Key)
                  tar_file_obj = s3.get_object(Bucket='abc-logs',Key=Key)
                  # print(tar_file_obj)
                  # tar = tarfile.open(tar_file_obj[&quot;Body&quot;],&quot;r:gz&quot;)
                  # print(tar)
                  file_content = tar_file_obj[&quot;Body&quot;].iter_lines()
                  print(file_content)
  </code></pre>