如何通过Amazon Connect将客户语音消息存储至S3存储桶
Got it, let's break down exactly how to store voice messages from your Amazon Connect contact flow into an S3 bucket, and crucially, tie each recording to the customer's phone number. Since you already have your contact workflow built, we'll focus on the storage and mapping pieces you're missing.
First, you need to enable recording within your existing contact flow. Here's how:
- Open your Amazon Connect console, navigate to Contact Flows, and open the flow you're using.
- Drag and drop the
Set recording behaviorblock into your flow (place it early, ideally right after the customer connects). - Configure it to record either the entire call or specific segments (like when the customer leaves a voicemail, if that's your use case).
- Make sure to set the recording trigger correctly—for voicemails, you might use the
When customer is prompted to leave a messageoption.
To link recordings to customer numbers, you need to grab that data from the contact attributes. You can do this two ways:
- Directly in the contact flow: Use the
Set contact attributesblock to store the customer's number as a custom attribute. The variable for the incoming customer number is$.CustomerEndpoint.Address(for outbound calls, use$.SystemEndpoint.Addressif you're initiating the call to the customer). - With a Lambda function: If you need to process the number (like formatting it, stripping country codes), add a
Invoke AWS Lambda functionblock to your flow. Pass the$.CustomerEndpoint.Addressas a parameter to Lambda, then have Lambda return the cleaned number as a contact attribute.
Now, set up the connection between Amazon Connect and your S3 bucket:
- Go to your Amazon Connect instance settings, select Data storage.
- Under Call recordings, click Edit and choose your target S3 bucket.
- Here's the key part: Customize the S3 prefix to include the customer number. Use the contact attribute you created earlier—for example, if you named your attribute
customerPhone, the prefix could be:
This way, each recording's folder path will include the customer's number, making it easy to locate and associate later.recordings/{instanceId}/{contactId}/{customerPhone}/
If you want to attach the customer number as metadata directly to the S3 object (instead of just in the path), use an EventBridge rule and Lambda:
- Create an EventBridge rule that triggers when Amazon Connect finishes a recording (the event type is
AWS::Connect::ContactRecording). - Configure the rule to invoke a Lambda function. The Lambda will receive event data containing the
contactId,recordingS3Uri, and the contact attributes (including the customer number). - In the Lambda code, use the AWS SDK to update the S3 object's metadata. For example, in Python:
import boto3 s3 = boto3.client('s3') def lambda_handler(event, context): recording_uri = event['detail']['recordingS3Uri'] bucket = recording_uri.split('/')[2] key = '/'.join(recording_uri.split('/')[3:]) customer_phone = event['detail']['contactAttributes']['customerPhone'] s3.copy_object( Bucket=bucket, Key=key, CopySource={'Bucket': bucket, 'Key': key}, Metadata={'customer-phone': customer_phone}, MetadataDirective='REPLACE' ) return {'statusCode': 200} - Make sure your Lambda has permissions to read from EventBridge, access the S3 bucket, and modify object metadata.
Grab a test phone and call into your contact flow. After the call ends, check your S3 bucket:
- Confirm the recording file exists in the prefix you set, including the customer number.
- If you added metadata, view the S3 object's properties to verify the
customer-phonemetadata is present.
Quick Notes on Permissions
- Ensure your Amazon Connect instance has an IAM role with
s3:PutObjectpermissions for your target bucket. - If using Lambda, the Lambda execution role needs permissions for
connect:GetContactAttributes,s3:GetObject,s3:PutObject, andevents:PutRule.
内容的提问来源于stack exchange,提问作者Mohammad Shahadat Hossain




