Use Media Files with Your Alexa-Hosted Skill
When you create an Alexa-hosted skill, Alexa stores your code and resources on AWS for you. For an overview of hosted skills, see About Alexa-hosted Skills.
When you create an Alexa-hosted skill, you get access to an Amazon S3 bucket for media storage. Your S3 bucket is part of the AWS free tier that provides storage free of charge within the usage limits. All media files stored in your S3 bucket are fully encrypted at rest with encryption keys managed by AWS Key Management Service (KMS). For details, see Protecting S3 data using server-side encryption with AWS KMS.
You can use the following S3 actions: GetObject, PutObject, DeleteObject, ListBucket, ListAllMyBuckets. If public read access is necessary for a file, use a pre-signed URL as shown in the examples later in this topic.
View your Amazon S3 bucket
To see the Amazon S3 bucket for your Alexa-hosted skill
- Open the Alexa developer console and log in.
- In the list of your skills, click the name of the skill that you want to view.
- Click the Code tab.
- In the toolbar, click the Media icon to open the AWS S3 console.
Your Amazon S3 bucket name and Media/ folder appear.
Use media files with Node.js
To access a file on Amazon S3 from the index.js
for your skill, require util.js
and then get the pre-signed URL by using the utility function getS3PreSignedUrl()
. The URL expires in 60 seconds, and you can't override the expiration from the utility function.
For more details about media file format, see Speech Synthesis Markup Language (SSML) Reference.
Picture file example
The following code example gets the file picture.jpg
from the S3 bucket.
const Util = require('./util.js');
handle(handlerInput)
{
const pictureUrl = Util.getS3PreSignedUrl("Media/picture.jpg");
return handlerInput.responseBuilder
.speak('hello world with picture')
.withStandardCard('card title', 'card text', pictureUrl)
.getResponse();
}
Audio file example
The following example gets the file audio.mp3
from the S3 bucket. For more details about the audio tag, see SSML audio.
const Util = require('./util.js');
handle(handlerInput)
{
const audioUrl = Util.getS3PreSignedUrl("Media/audio.mp3");
return handlerInput.responseBuilder
.speak(`hello world with audio <audio src="${audioUrl}"/>`)
.getResponse();
}
Use media files with Python
For more details about media file format and the audio tag, see Speech Synthesis Markup Language (SSML) Reference.
Picture file example
The following example gets the file image.jpg
from the S3 bucket.
from ask_sdk_model import ui
from utils import create_presigned_url
def handle(self, handler_input):
# type: (HandlerInput) -> Response
image_url = create_presigned_url("Media/image.png")
return (
handler_input.response_builder
.speak("hello world with picture")
.set_card(ui.StandardCard(title="Card Title",
text="Hi, this is a sample card",
image=ui.Image(small_image_url=image_url, large_image_url=image_url)))
.response
)
Audio file example
The following example gets the file audio.m4a
from the S3 bucket.
You can also enable the audio interface in the Build tab. For details, see AudioPlayer Interface Reference. For details about PlayDirective
, see Alexa Skills Kit SDK for Python.
from ask_sdk_model.interfaces.audioplayer import AudioItem, Stream, PlayDirective, PlayBehavior
from utils import create_presigned_url
def handle(self, handler_input):
# type: (HandlerInput) -> Response
audio_url = create_presigned_url("Media/audio.m4a")
handler_input.response_builder
.add_directive(PlayDirective(play_behavior=PlayBehavior.REPLACE_ALL,
audio_item=AudioItem(stream=Stream(token="1234AAAABBBBCCCCCDDDDEEEEEFFFF",
url=audio_url, offset_in_milliseconds=10, expected_previous_token=None))))
return (
handler_input.response_builder.response
)
Related topics
- Use DynamoDB for Data Persistence with Your Alexa-hosted Skill
- Use Personal AWS Resources with Your Alexa-hosted Skill
Last updated: May 01, 2024