-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpf.py
181 lines (143 loc) · 5.06 KB
/
mpf.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
--code--
import json
import boto3
import cgi
from io import BytesIO
s3 = boto3.client('s3')
def lambda_handler(event, context):
try:
# Extract the bucket name from the path parameters
bucket_name = "bucketchy"
# event['pathParameters']['bucket']
# Extract the Content-Type header
content_type_header = event['headers'].get('Content-Type', event['headers'].get('content-type'))
if not content_type_header:
return {
'statusCode': 400,
'body': json.dumps({'message': 'Content-Type header missing'})
}
# Extract the boundary string from the Content-Type header
boundary = content_type_header.split("boundary=")[1]
# Extract the body of the request
body = event['body']
# Parse the multipart form data using the boundary string
environ = {'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': f'multipart/form-data; boundary={boundary}'}
fp = BytesIO(body.encode())
form = cgi.FieldStorage(fp=fp, environ=environ, keep_blank_values=True)
# Check if a file part exists in the form data
if 'file' not in form:
return {
'statusCode': 400,
'body': json.dumps({'message': 'No file part in the request'})
}
file_item = form['file']
if not file_item.file:
return {
'statusCode': 400,
'body': json.dumps({'message': 'File not found in the request'})
}
file_name = file_item.filename
file_content = file_item.file.read()
# Perform the upload to S3
s3.put_object(Bucket=bucket_name, Key=file_name, Body=file_content)
return {
'statusCode': 200,
'body': json.dumps({'message': f'File {file_name} uploaded successfully to bucket {bucket_name}'})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'message': str(e)})
}
----------------
model schema
-----
{
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
},
"required": ["file"]
}
----------------
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:role"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::name/*"
}
]
}
----------
curl -X POST api \
-H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryABC123" \
--data-binary @abcd.txt
------ abcd.txt
------WebKitFormBoundaryABC123
Content-Disposition: form-data; name="file"; filename="abcd.txt"
Content-Type: text/plain
This is the content of the file.
It can be multiple lines.
End of file content.
------WebKitFormBoundaryABC123--
import json
import boto3
import cgi
from io import BytesIO
s3 = boto3.client('s3')
def lambda_handler(event, context):
try:
# Extract the bucket name from the path parameters
bucket_name = "bucketchy"
# event['pathParameters']['bucket']
# Extract the Content-Type header
content_type_header = event['headers'].get('Content-Type', event['headers'].get('content-type'))
if not content_type_header:
return {
'statusCode': 400,
'body': json.dumps({'message': 'Content-Type header missing'})
}
# Extract the boundary string from the Content-Type header
boundary = content_type_header.split("boundary=")[1]
# Extract the body of the request
body = event['body']
# Parse the multipart form data using the boundary string
environ = {'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': f'multipart/form-data; boundary={boundary}'}
fp = BytesIO(body.encode())
form = cgi.FieldStorage(fp=fp, environ=environ, keep_blank_values=True)
# Check if a file part exists in the form data
if 'file' not in form:
return {
'statusCode': 400,
'body': json.dumps({'message': 'No file part in the request'})
}
file_item = form['file']
if not file_item.file:
return {
'statusCode': 400,
'body': json.dumps({'message': 'File not found in the request'})
}
file_name = file_item.filename
file_content = file_item.file.read()
# Perform the upload to S3
s3.put_object(Bucket=bucket_name, Key=file_name, Body=file_content)
return {
'statusCode': 200,
'body': json.dumps({'message': f'File {file_name} uploaded successfully to bucket {bucket_name}'})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'message': str(e)})
}
curl -X POST https://your-api-id.execute-api.your-region.amazonaws.com/upload \
-H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryABC123" \
--data-binary @multipart-data.txt