Access Zip File from S3

MPJ567

I have code that I use to extract a csv that's inside a zip folder with nested folders. This code works great when the zip file is local but we are moving the files to our S3 bucket. I'd like to apply my existing code within an AWS lambda function however I'm unsure how to access the zip file from S3. This code was originally designed to operate on multiple zip files hence the looping, in this new case it will only be a single zip file for but simplicities sake I've just copied existing code with loop.

Code snippet with missing code below:

all_files = NEED CODE THAT GRABS ZIP FILE STORED ON S3 

for file in all_files:
    with ZipFile(file) as zip:
        for zip_info in zip.infolist():
           if zip_info.filename[-1] == '/':
              continue
           zip_info.filename = os.path.basename(zip_info.filename)
           master_df = pd.read_csv(zip.open(zip_info))


master_df = pd.concat((pd.read_csv(f, encoding = 'ANSI',dtype='str') for f in all_files), ignore_index=True)
master_df.drop(master_df.columns[0],axis=1, inplace= True)
master_df.columns = [x.replace("\n", "") for x in 
master_df.columns.str.strip().to_list()]
master_df.rename(columns = {'Date':'REPORT_PERIOD', 'Ad Impressions':'IMPRESSIONS', 'Publisher Currency Revenue':'REVENUE'}, inplace=True)
John Rotenstein

The easiest method would be to use download_file() to download the Zip file to the /tmp/ directory (which is the only location that is writeable in AWS Lambda functions).

For example:

import boto3
s3_client = boto3.client('s3')
s3_client.download_file('bucket-name', 'file.zip', '/tmp/file.zip')

Your code can then access the local /tmp/file.zip file as normal.

Also, please note that the Lambda function environment might be reused for future function executions. Therefore, it is generally a good idea to delete any downloaded files otherwise it might consume all 512MB of storage available.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Zip File from S3 files

From

Create a zip file on S3 from files on S3 using Lambda Node

From Dev

How to access a file on Amazon S3 from the Command Line?

From Dev

How to solve s3 access denied on file added to s3 bucket from another account?

From Dev

How to push all the zip files from my computer desktop to s3 bucket (Public Access)

From Java

Selectively extract entries from a zip file in S3 without downloading entire file

From Java

Unzip ZIP file on Amazon S3

From Java

Read ZIP files from S3 without downloading the entire file

From Dev

nodejs: how to download a zip file from AWS S3 bucket, save as local file in EC2 instance and unzip it?

From Dev

Send File S3 Access Denied

From Dev

Boto3 not uploading zip file to S3 python

From Dev

FIle upload from browser to S3 which has block public access

From Dev

Permission Denied Access S3 file from Zeppelin installed on EMR

From Dev

Unable to upload file from Elastic Beanstalk to s3 due to access denied

From Dev

How to access to docker config file in S3 bucket from Elastic Beanstalk instance

From Dev

Unable to access S3 file with IAM role from EC2

From Dev

Amazon S3 File Permissions, Access Denied when copied from another account

From Dev

How to access css/js file from S3 in AWS lambda function

From Dev

How to access a zipEntry from a streamed zip file in memory

From Dev

Access S3 bucket from VPC

From Dev

Access video from aws s3

From Dev

terraform to generate a file, zip and upload to s3

From Dev

AWS S3 - Zip only file objects not path

From Java

How to load zip file that resides in S3 bucket?

From Dev

AWS S3 ZIP file upload is corrupting the archived files

From Dev

Save zip file in storage s3 aws laravel 9

From Dev

How to upload zip file through S3 signed URL

From Dev

Laravel - S3 multiple file zip and download

From Dev

How to Stream files into a Zip from AWS S3

Related Related

  1. 1

    Zip File from S3 files

  2. 2

    Create a zip file on S3 from files on S3 using Lambda Node

  3. 3

    How to access a file on Amazon S3 from the Command Line?

  4. 4

    How to solve s3 access denied on file added to s3 bucket from another account?

  5. 5

    How to push all the zip files from my computer desktop to s3 bucket (Public Access)

  6. 6

    Selectively extract entries from a zip file in S3 without downloading entire file

  7. 7

    Unzip ZIP file on Amazon S3

  8. 8

    Read ZIP files from S3 without downloading the entire file

  9. 9

    nodejs: how to download a zip file from AWS S3 bucket, save as local file in EC2 instance and unzip it?

  10. 10

    Send File S3 Access Denied

  11. 11

    Boto3 not uploading zip file to S3 python

  12. 12

    FIle upload from browser to S3 which has block public access

  13. 13

    Permission Denied Access S3 file from Zeppelin installed on EMR

  14. 14

    Unable to upload file from Elastic Beanstalk to s3 due to access denied

  15. 15

    How to access to docker config file in S3 bucket from Elastic Beanstalk instance

  16. 16

    Unable to access S3 file with IAM role from EC2

  17. 17

    Amazon S3 File Permissions, Access Denied when copied from another account

  18. 18

    How to access css/js file from S3 in AWS lambda function

  19. 19

    How to access a zipEntry from a streamed zip file in memory

  20. 20

    Access S3 bucket from VPC

  21. 21

    Access video from aws s3

  22. 22

    terraform to generate a file, zip and upload to s3

  23. 23

    AWS S3 - Zip only file objects not path

  24. 24

    How to load zip file that resides in S3 bucket?

  25. 25

    AWS S3 ZIP file upload is corrupting the archived files

  26. 26

    Save zip file in storage s3 aws laravel 9

  27. 27

    How to upload zip file through S3 signed URL

  28. 28

    Laravel - S3 multiple file zip and download

  29. 29

    How to Stream files into a Zip from AWS S3

HotTag

Archive