Created
August 18, 2022 06:23
-
-
Save ojmccall/0369d37331df80be3ed8f464e73c3027 to your computer and use it in GitHub Desktop.
Lambda to trigger SFMC import from S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib.parse | |
import boto3 | |
import requests | |
client_id = "client id here " | |
client_secret = "client secret here" | |
baseURL = "unique url string here" | |
automationURL = f"https://{baseURL}.rest.marketingcloudapis.com/automation/v1/automations/trigger" | |
#get reference data | |
refBucket = "reference bucket name" | |
refFilename = "reference file name eg reference.json" | |
def lambda_handler(event, context): | |
#get event details | |
s3 = boto3.client('s3') | |
bucket = event['Records'][0]['s3']['bucket']['name'] | |
filename = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') | |
#create lookup for reference data | |
split = filename.split("_") | |
lookupFilename = split[0] | |
s3 = boto3.resource('s3') | |
obj = s3.Object(refBucket, refFilename) | |
data = json.load(obj.get()['Body']) | |
fileLocationKey = data[lookupFilename] | |
#get token | |
url = f"https://{baseURL}.auth.marketingcloudapis.com/v2/Token" | |
data = {"grant_type":"client_credentials", | |
"client_id":client_id, | |
"client_secret":client_secret | |
} | |
r = requests.post(url, data=data) | |
body = json.loads(r.content) | |
token = body['access_token'] | |
#post to SFMC with Auth to trigger automation | |
headers = {"Authorization": "Bearer " + token} | |
data = { | |
"fileTransferLocationKey": fileLocationKey, | |
"filename": filename | |
} | |
r = requests.post(automationURL, data=data,headers = headers) | |
body = json.loads(r.content) | |
print(body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment