-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3_file_list.py
35 lines (29 loc) · 1.27 KB
/
s3_file_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import print_function
import json
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
s3obj = boto3.resource('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).encode('utf8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
extension = key.split(".")[-1]
print(response['Body'].read())
print("adding new file name to list")
s3.download_file('logs', 'file-track.csv', '/tmp/file-track.csv')
text_file = open("/tmp/file-track.csv", "a")
text_file.write("%s,%s\n" % (extension,key))
text_file.close()
print("finished adding new file - uploading")
s3.upload_file(Filename='/tmp/file-track.csv', Bucket='logs', Key='file-track.csv')
print("FILE NAME: " + key)
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e