前提・実現したいこと
awsのlambda上でpandasを使いたく、ここのzipをS3に持ってきて、tmpフォルダにzipファイルをアップロードして解凍したいのですが、unzipコマンドが効かずに困っています
発生している問題・エラーメッセージ
Response: null Request ID: "e5714beb-bcb5-11e8-a4c8-73f33fd3e079" Function Logs: START RequestId: e5714beb-bcb5-11e8-a4c8-73f33fd3e079 Version: $LATEST total 8 -rw-rw-r-- 1 sbx_user1086 460 190 Sep 20 09:16 lambda.zip drwxrwxr-x 3 sbx_user1086 460 4096 Sep 20 09:16 log total 8 -rw-rw-r-- 1 sbx_user1086 460 190 Sep 20 09:16 lambda.zip drwxrwxr-x 3 sbx_user1086 460 4096 Sep 20 09:16 log /usr/bin/unzip total 8 -rwxrwxr-x 1 sbx_user1086 460 190 Sep 20 09:16 lambda.zip drwxrwxr-x 3 sbx_user1086 460 4096 Sep 20 09:16 log END RequestId: e5714beb-bcb5-11e8-a4c8-73f33fd3e079 REPORT RequestId: e5714beb-bcb5-11e8-a4c8-73f33fd3e079 Duration: 1353.87 ms Billed Duration: 1400 ms Memory Size: 128 MB Max Memory Used: 34 MB
該当のソースコード
python
1import boto3 2import os 3import os.path 4import tarfile 5import shutil 6 7 8def lambda_handler(event, context): 9 bucket_name = 'test-df' 10 tmp_dir = '/tmp/' 11 log_dir = '/tmp/log/sess/' 12 file_name = 'lambda.zip' 13 14 # ここはS3側 15 key = file_name 16 file_path = tmp_dir + file_name 17 18 # === Step 1 19 # create log dir 20 if os.path.exists(log_dir) == False: 21 os.makedirs(log_dir) 22 23 # write log 24 with open(log_dir + 'log1.txt', 'w') as file: 25 file.write('hoge\n') 26 with open(log_dir + 'log2.txt', 'w') as file: 27 file.write('fuga\n') 28 29 30 # create log archive 31 with tarfile.open(file_path, mode='w:gz') as archive: 32 archive.add(log_dir) 33 34 # === Step 2 35 # create s3 resource 36 s3 = boto3.resource('s3') 37 bucket = s3.Bucket(bucket_name) 38 39 # upload log archive 40 bucket.upload_file(file_path, key) 41 42 # === Step 3 43 # delete log archive 44 if os.path.exists(file_path): 45 os.remove(file_path) 46 47 # download log archive 48 bucket.download_file(key, file_path) 49 50 import subprocess 51 52 out = subprocess.run(['ls', '-l', '../../tmp/'], stdout=subprocess.PIPE) 53 print(out.stdout.decode()) 54 out = subprocess.run(['ls', '-l', '/tmp/'], stdout=subprocess.PIPE) 55 print(out.stdout.decode()) 56 # 解凍するために権限付与 57 subprocess.run(['chmod', '775', '/tmp/lambda.zip'], stdout=subprocess.PIPE) 58 59 60 out = subprocess.run(['which', 'unzip'], stdout=subprocess.PIPE) 61 print(out.stdout.decode()) 62 63 # エラー箇所 というかこのコマンドで実行ができない。 64 subprocess.run(['unzip', '/tmp/lambda.zip']) 65 out = subprocess.run(['ls', '-l', '../../tmp/'], stdout=subprocess.PIPE) 66 print(out.stdout.decode()) 67 # print() 68 69 # === Step 4 70 # delete log dir 71 if os.path.exists(log_dir): 72 shutil.rmtree(log_dir) 73 74 # extract log archive 75 with tarfile.open(file_path, mode='r:gz') as archive: 76 archive.extractall('/') 77
試したこと
which unzipは通っているのでコマンドはあるようなのですが、解凍したディレクトリができません。
この方法でなくともlambdaでpandas, sikitlearnを使う方法があれば教えていただきたいです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/22 09:47