-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnzipS3FunctionTemplate.yaml
134 lines (119 loc) · 4.49 KB
/
UnzipS3FunctionTemplate.yaml
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
AWSTemplateFormatVersion: '2010-09-09'
Resources:
s3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
AccessControl: 'PublicRead'
BucketName: 'testunzipbucket04102019-215341'
WebsiteConfiguration:
ErrorDocument: 'error.html'
IndexDocument: 'index.html'
s3BucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref s3Bucket
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
- 's3:PutObject'
- 's3:GetObjectVersion'
- 's3:ListBucket'
Effect: Allow
Principal:
AWS:
- '*'
Resource:
- !Join
- ''
- - 'arn:aws:s3:::'
- !Ref s3Bucket
- '/*'
- !Join
- ''
- - 'arn:aws:s3:::'
- !Ref s3Bucket
Version: '2012-10-17'
lambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile: !Sub |
# inline
#based on unzipcode here: https://www.quora.com/How-do-I-extract-a-zip-file-in-an-Amazon-S3-by-using-Lambda
from __future__ import print_function
import urllib
import boto3
import os
import zipfile
lambda_client = boto3.client('lambda')
#-------- S3 variables -----------
s3_resource= boto3.resource('s3')
s3_client= boto3.client('s3')
#-------- global variables -------
tmpFolder = '/tmp/'
unzipTmpFile='file.zip'
deletedZipFolder='deletedZipFolder/'
extension = ".zip"
#-------- functions begin---------
def lambda_handler(event, context):
print(event)
bucketName = event['bucketName']
key = event['key']
destinationFolder = event['destinationFolder']
bucket = s3_resource.Bucket(bucketName)
try:
print('bucketName:{} key:{} '.format(bucketName, key))
unzipAttachment(bucketName, key, destinationFolder)
return('success')
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
return(e)
#--------- unzip files ----------
def unzipAttachment(bucketName, key, destinationFolder):
print('unzipAttachment {} {} {}'.format(bucketName, key, destinationFolder))
s3_client.download_file(bucketName, key, tmpFolder+unzipTmpFile)
dir_name = tmpFolder
os.chdir(dir_name)
for item in os.listdir(tmpFolder):
if item.endswith(extension):
file_name = os.path.abspath(item)
zip_ref = zipfile.ZipFile(file_name)
zip_ref.extractall(dir_name)
zip_ref.close()
os.remove(file_name)
removeFolderStucture(bucketName, key, destinationFolder)
#--------- removes nested folder structure----------
def removeFolderStucture( bucketName, key, destinationFolder):
for p, d, f in os.walk(tmpFolder, topdown=False):
for n in f:
os.rename(os.path.join(p, n), os.path.join(tmpFolder, n))
for n in d:
os.rmdir(os.path.join(p, n))
for iitem in os.listdir(tmpFolder):
file_name = os.path.abspath(iitem)
s3_client.upload_file(tmpFolder+iitem, bucketName, '{}/{}'.format(destinationFolder, iitem))
os.remove(file_name)
FunctionName: 'testunziplambda04102019-215341'
Handler: 'index.lambda_handler'
Role:
Fn::GetAtt:
- lambdaRole
- Arn
Runtime: 'python3.7'
lambdaRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- 'lambda.amazonaws.com'
Version: '2012-10-17'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AWSLambdaExecute'