Skip to content

Instantly share code, notes, and snippets.

@ensean
Created August 31, 2023 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ensean/44a5b0e03b888b7a63e63f84d8b767c3 to your computer and use it in GitHub Desktop.
Save ensean/44a5b0e03b888b7a63e63f84d8b767c3 to your computer and use it in GitHub Desktop.
lambda_auto_unzip
import logging
import zipfile
import mimetypes
from io import BytesIO
from boto3 import resource
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def upload_files(zipped, zipfile_info, destinationbucket, upload_path):
file_path = zipfile_info.filename
full_path = upload_path + '/' + file_path # Construct full path
if zipfile_info.is_dir(): # If it's a directory, create it in S3
destinationbucket.put_object(Key=full_path) # Trailing slash indicates a directory
else: # If it's a file, upload it
mtype = mimetypes.guess_type(full_path)[0]
if not mtype:
mtype = 'application/octet-stream'
with zipped.open(zipfile_info, "r") as f_in:
destinationbucket.upload_fileobj(f_in, full_path, ExtraArgs = {'ContentType': mtype})
def unzip_and_upload_directory(filekey, sourcebucketname, destinationbucket, upload_path):
try:
zipped_file = s3_resource.Object(bucket_name=sourcebucketname, key=filekey)
buffer = BytesIO(zipped_file.get()["Body"].read())
zipped = zipfile.ZipFile(buffer)
for zfile_info in zipped.infolist():
logger.info(f'current file in zipfile: {zfile_info.filename}')
upload_files(zipped, zfile_info, destinationbucket, upload_path)
except Exception as e:
logger.info(f'Error: Unable to upload directory: {e}')
def lambda_handler(event, context):
global s3_resource
s3_resource = resource('s3')
sourcebucketname = 'sagemaker-us-west-2-fsdfdsf'
destination_bucket = s3_resource.Bucket('sagemaker-us-west-2-fsdfdsf')
upload_path = 'test' # Specify the upload path here
key = event['Records'][0]['s3']['object']['key']
unzip_and_upload_directory(key, sourcebucketname, destination_bucket, upload_path)
@ensean
Copy link
Author

ensean commented Sep 1, 2023

删除原始文件

source_bucket = s3_resource.Bucket(sourcebucketname)
source_bucket.delete_objects(    Delete={
        'Objects': [
            {
                'Key': key
            },
        ],
        'Quiet': True
    },)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment