質問編集履歴

3

goji

2019/11/20 08:55

投稿

skyhigh
skyhigh

スコア12

test CHANGED
File without changes
test CHANGED
@@ -181,117 +181,3 @@
181
181
 
182
182
 
183
183
  ```
184
-
185
-
186
-
187
-
188
-
189
- **11/20追記**
190
-
191
-
192
-
193
-
194
-
195
- こちらのようにすれば解決しました。
196
-
197
-
198
-
199
-
200
-
201
- ```PYTHON
202
-
203
- import boto3
204
-
205
- import tarfile
206
-
207
- import traceback
208
-
209
- import os
210
-
211
-
212
-
213
- print('Loading function')
214
-
215
-
216
-
217
- s3 = boto3.resource('s3')
218
-
219
- s3_client = boto3.client('s3')
220
-
221
-
222
-
223
- def lambda_handler(event, context):
224
-
225
-
226
-
227
- # Get the object from the event and show its content type
228
-
229
- bucket = event['Records'][0]['s3']['bucket']['name']
230
-
231
- key = event['Records'][0]['s3']['object']['key']
232
-
233
-
234
-
235
- try:
236
-
237
- s3_client.download_file(bucket, key, '/tmp/file.tar')
238
-
239
- print('download')
240
-
241
- zfile = tarfile.TarFile('/tmp/file.tar')
242
-
243
- namelist = zfile.getnames()
244
-
245
- print(namelist)
246
-
247
- for filename in namelist:
248
-
249
- if not os.path.basename('/tmp/'+filename):
250
-
251
- os.mkdir('/tmp/'+filename)
252
-
253
- else:
254
-
255
- f = open('/tmp/' + str(filename), 'wb')
256
-
257
-
258
-
259
-
260
-
261
- #修正箇所
262
-
263
- data = zfile.extractfile(filename).read()
264
-
265
-
266
-
267
- f.write(data)
268
-
269
- f.close()
270
-
271
-
272
-
273
- for filename in namelist:
274
-
275
- s3_client.upload_file('/tmp/'+filename, bucket, filename)
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
- except Exception as e:
286
-
287
- print(e)
288
-
289
- print(traceback.format_exc())
290
-
291
- ```
292
-
293
-
294
-
295
-
296
-
297
- ありがとうございました。

2

解決方法

2019/11/20 08:55

投稿

skyhigh
skyhigh

スコア12

test CHANGED
File without changes
test CHANGED
@@ -186,7 +186,7 @@
186
186
 
187
187
 
188
188
 
189
- 11/20追記
189
+ **11/20追記**
190
190
 
191
191
 
192
192
 

1

解決方法

2019/11/20 02:14

投稿

skyhigh
skyhigh

スコア12

test CHANGED
File without changes
test CHANGED
@@ -181,3 +181,117 @@
181
181
 
182
182
 
183
183
  ```
184
+
185
+
186
+
187
+
188
+
189
+ 11/20追記
190
+
191
+
192
+
193
+
194
+
195
+ こちらのようにすれば解決しました。
196
+
197
+
198
+
199
+
200
+
201
+ ```PYTHON
202
+
203
+ import boto3
204
+
205
+ import tarfile
206
+
207
+ import traceback
208
+
209
+ import os
210
+
211
+
212
+
213
+ print('Loading function')
214
+
215
+
216
+
217
+ s3 = boto3.resource('s3')
218
+
219
+ s3_client = boto3.client('s3')
220
+
221
+
222
+
223
+ def lambda_handler(event, context):
224
+
225
+
226
+
227
+ # Get the object from the event and show its content type
228
+
229
+ bucket = event['Records'][0]['s3']['bucket']['name']
230
+
231
+ key = event['Records'][0]['s3']['object']['key']
232
+
233
+
234
+
235
+ try:
236
+
237
+ s3_client.download_file(bucket, key, '/tmp/file.tar')
238
+
239
+ print('download')
240
+
241
+ zfile = tarfile.TarFile('/tmp/file.tar')
242
+
243
+ namelist = zfile.getnames()
244
+
245
+ print(namelist)
246
+
247
+ for filename in namelist:
248
+
249
+ if not os.path.basename('/tmp/'+filename):
250
+
251
+ os.mkdir('/tmp/'+filename)
252
+
253
+ else:
254
+
255
+ f = open('/tmp/' + str(filename), 'wb')
256
+
257
+
258
+
259
+
260
+
261
+ #修正箇所
262
+
263
+ data = zfile.extractfile(filename).read()
264
+
265
+
266
+
267
+ f.write(data)
268
+
269
+ f.close()
270
+
271
+
272
+
273
+ for filename in namelist:
274
+
275
+ s3_client.upload_file('/tmp/'+filename, bucket, filename)
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+ except Exception as e:
286
+
287
+ print(e)
288
+
289
+ print(traceback.format_exc())
290
+
291
+ ```
292
+
293
+
294
+
295
+
296
+
297
+ ありがとうございました。