Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@kitamuratomokazu

S3にuploadしたzipファイルのイメージ画像をLambdaで解凍する メモ

More than 3 years have passed since last update.
extract_image_zip.py

import boto3
import zipfile
import traceback
import os

print('Loading function')

s3 = boto3.resource('s3')
s3_client = boto3.client('s3')

def lambda_handler(event, context):

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']

    try: 
        s3_client.download_file(bucket, key, '/tmp/file.zip')
        print('download')
        zfile = zipfile.ZipFile('/tmp/file.zip')
        namelist = zfile.namelist()
        print(namelist)

        for filename in namelist:
            if not os.path.basename('/tmp/'+filename):
                os.mkdir('/tmp/'+filename)
            else:
                f = open('/tmp/' + str(filename), 'wb')
                data = zfile.read(filename)
                f.write(data)
                f.close()
        for filename in namelist:
            if '.jpg' in filename:
                s3_client.upload_file('/tmp/'+filename, bucket, key + filename)


    except Exception as e:
        print(e)
        print(traceback.format_exc())

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
scrumsign
機械学習のモデリングおよび、数学と機械学習のコミュニティー 「Math & Coding」の運営を行なっています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?