41 lines
863 B
Python
41 lines
863 B
Python
import os
|
|
import json
|
|
|
|
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
TOPIC = os.getenv("MQTT_TOPIC", "")
|
|
BUCKET = os.getenv("BUCKET", "")
|
|
|
|
s3 = boto3.client('s3')
|
|
iot_data = boto3.client('iot-data')
|
|
|
|
# trigger by MQTT topic events
|
|
def lambda_handler(event, _context):
|
|
filename = event['filename']
|
|
|
|
try:
|
|
result = s3.generate_presigned_put(BUCKET, filename)
|
|
print("Got presigned URL: {}".format(result['url']))
|
|
except ClientError as e:
|
|
print(e)
|
|
print("Couldn't get a presigned URL")
|
|
raise e
|
|
|
|
try:
|
|
iot_publish(result)
|
|
except Exception as e:
|
|
print(e)
|
|
print(f"Error while publishing to MQTT topic {TOPIC}.")
|
|
raise e
|
|
|
|
return url
|
|
|
|
|
|
def iot_publish(payload):
|
|
iot_data.publish(
|
|
topic=TOPIC,
|
|
qos=0,
|
|
payload=json.dumps(payload)
|
|
)
|