前提
表題の通りです。
参考にしたコード(URL:https://qiita.com/kitamuratomokazu/items/2c9fe087dfdca4482788)
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: s3_client.upload_file('/tmp/'+filename, bucket, key + filename) except Exception as e: print(e) print(traceback.format_exc())
実現したいこと
・AWS環境でLambda⇒S3をトリガーにS3にアップロードしたzipファイルを自動的に解凍したいです。
・前提ではJPGですが、今回の場合ですがzipフォルダーには例えば
testフォルダ
|-A.txt
|-B.xlsx
という階層になっています。
参考にしているコードだとJPGファイル単体をzipファイルを解凍できるのですが
フォルダを挟んだ場合の複数ファイルだとできません。Cloudwatch Logに以下通りのエラーが表示されました。
発生している問題・エラーメッセージ
message "Loading function "START RequestId: cfceab81-1e66-4a10-aa0e-a720c613b635 Version: $LATEST "download "['test/A.txt', 'test/B.xlsx'] "[Errno 2] No such file or directory: '/tmp/test/A.txt' "Traceback (most recent call last): "File ""/var/task/lambda_function.py"", line 33, in lambda_handler "f = open('/tmp/' + str(filename), 'wb') "FileNotFoundError: [Errno 2] No such file or directory: '/tmp/test/A.txt' "END RequestId: cfceab81-1e66-4a10-aa0e-a720c613b635 "REPORT RequestId: cfceab81-1e66-4a10-aa0e-a720c613b635 Duration: 383.95 ms Billed Duration: 384 ms Memory Size: 128 MB Max Memory Used: 73 MB Init Duration: 408.20 ms
試したこと
zip解凍後、Lambdaの「/tmp/」に格納される流れですが、「/tmp/」だとOKですが
「/tmp/test」だとディレクトリが見つかりませんと表示されるので、
zipで展開したらファイルを移動させればいいと思い、以下通りにしてみました。
#(5行目に追加) import shutil ... zfile = zipfile.ZipFile('/tmp/file.zip') namelist = zfile.namelist() print(namelist) #(追加部分)展開したファイルをtmp配下に移動する shutil.move(namelist, '/tmp/') for filename in namelist: if not os.path.basename('/tmp/'+filename): os.mkdir('/tmp/'+filename) ...
しかしログには以下通りに出力されました。
message Loading function START RequestId: e1e5472e-35a4-461e-a5af-a5c0c204d8ad Version: $LATEST download ['test/A.txt', 'test/B.xlsx'] stat: path should be string, bytes, os.PathLike or integer, not list Traceback (most recent call last): File ""/var/task/lambda_function.py"", line 27, in lambda_handler shutil.move(namelist, '/tmp/') File ""/var/lang/lib/python3.9/shutil.py"", line 812, in move if _samefile(src, dst): File ""/var/lang/lib/python3.9/shutil.py"", line 220, in _samefile return os.path.samefile(src, dst) File ""/var/lang/lib/python3.9/genericpath.py"", line 100, in samefile s1 = os.stat(f1) TypeError: stat: path should be string, bytes, os.PathLike or integer, not list END RequestId: e1e5472e-35a4-461e-a5af-a5c0c204d8ad REPORT RequestId: e1e5472e-35a4-461e-a5af-a5c0c204d8ad Duration: 404.88 ms Billed Duration: 405 ms Memory Size: 128 MB Max Memory Used: 74 MB Init Duration: 431.47 ms
pythonをよく理解できず仕舞いですみませんが、
複数ファイルを展開する際に解凍したらファイルを/tmpに移動させるのか
または他の方法がよければご指摘頂きますようお願いします。
補足情報(FW/ツールのバージョンなど)
AWS Lambda , Python 3.9
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
退会済みユーザー
2022/10/25 08:34