使用 S3 存储桶中的音频文件时出现错误并且技能显示问题。
import logging
import os
import boto3
from botocore.exceptions import ClientError
def create_presigned_url(object_name):
"""Generate a presigned URL to share an S3 object with a capped expiration of 60
seconds
:param object_name: string
:return: Presigned URL as string. If error, returns None.
"""
s3_client = boto3.client('s3',
region_name=os.environ.get('S3_PERSISTENCE_REGION'),
config=boto3.session.Config(signature_version='s3v4',s3=
{'addressing_style': 'path'}))
try:
bucket_name = os.environ.get('S3_PERSISTENCE_BUCKET')#"aaudiobucket"
print(bucket_name)
response = s3_client.generate_presigned_url('get_object',
Params={'Bucket': bucket_name,
'Key': object_name},
ExpiresIn=300)
except ClientError as e:
logging.error(e)
return None
#The response contains the presigned URL
return response
class HelloWorldIntentHandler(AbstractRequestHandler):
"""Handler for Hello World Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("HelloWorldIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
mp3_url = create_presigned_url("Trimmed_calm_music.mp3")
print(mp3_url)
# speak_output = "Whats going on?"
speak_output = f" </speak> Please listen this audio clip: <audio
src='{mp3_url}'/> </speak>"
return (
handler_input.response_builder
.speak(speak_output)
.response
)
通过此代码,我从 S3 存储桶文件中获取 mp3_url,但音频未播放。 当我手动点击 URL 时,出现错误:
<Error>
<Code>AccessDenied</Code>
<Message>Request has expired</Message>
<X-Amz-Expires>300</X-Amz-Expires>
<Expires>2023-12-18T15:38:17Z</Expires>
<ServerTime>2023-12-18T17:16:25Z</ServerTime>
<RequestId>GWWSAT3EM9XT2STD</RequestId>
<HostId>1Itsk7kIaAsidH3+v3fGy6W6WjMA5nXeR7PuqIzew3SPpCV+MMvusQAcXRAxtKQlXBdqYlQ6bbI=
</HostId>
</Error>
获取从 s3 存储桶获取的 URL 时出错。
您似乎正在使用 utils.py 中 Alexa 托管技能提供的功能来生成预签名 URL。请注意,对于 Alexa 托管的技能,过期时间固定为 60 秒。即使您将“ExpiresIn”值设置为 300,该技能也始终会考虑 60 秒。
此外,传递给函数的“key”值并不完整。使用通过 Alexa 托管技能配置的 S3 存储桶时,“key”值以“Media/”开头,后跟文件名。例如:
mp3_url = create_presigned_url("Media/Trimmed_calm_music.mp3")
通过 Python 使用媒体文件(音频文件示例):https://developer.amazon.com/en-US/docs/alexa/hosted-skills/alexa-hosted-skills-media-files.html#audio-file -示例-1