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 more information about hosted skills, see Build a Skill End-to-end Using an Alexa-hosted Skill.

When you create an Alexa-hosted skill, you get access to an Amazon S3 bucket for media storage. Your Amazon S3 usage is limited to the free tier: 5 GB of storage, 20,000 get requests, 2,000 put requests, and 15 GB of data transfer out per month. Alexa-hosted skills use the encryption methods supported by AWS for media files. 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

  1. Open the Alexa developer console and log in.
  2. In the list of your skills, click the name of the skill that you want to view.
  3. Click the Code tab.
  4. In the lower left corner, click the link Media storage: Amazon S3. A limited view of the AWS console opens and your S3 bucket appears. Your bucket name and folder appear similar to amzn1-ask-skill-xxxxxxx-xxxx-buildsnapshotbucket-xxxxx/Media/.

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 using the utility function getS3PreSignedUrl(). The URL expires in 60 seconds, and you cannot override the expiration from the utility function.

Picture file example

The following code example gets the file picture.jpg.

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 code example gets the file audio.mp3.

const Util = require('./util.js');

handle(handlerInput)
{
  const audioUrl = Util.getS3PreSignedUrl("Media/audio.mp3").replace(/&/g,'&');

  return handlerInput.responseBuilder
    .speak(`hello world with audio <audio src="${audioUrl}"/>`)
    .getResponse();
}

Use media files with Python

Picture file example

The following code example gets the file image.jpg.

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 code example gets the file audio.m4a.

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
    )