버전 비교

  • 이 줄이 추가되었습니다.
  • 이 줄이 삭제되었습니다.
  • 서식이 변경되었습니다.

...

코드 블럭
languagepy
themeRDark
titleConverAudio
linenumberstrue
# -*- coding: utf-8 -*-
from __future__ import print_function

import boto3
import os
from contextlib import closing
from boto3.dynamodb.conditions import Key, Attr
import re

def lambda_handler(event, context):
    postId = event["Records"][0]["Sns"]["Message"]
    print ("Text to Speech function. Post ID in DynamoDB: ", postId)
    
    # Retrieving information about the post from DynamoDB table
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['DB_TABLE_NAME'])
    postItem = table.query(
        KeyConditionExpression=Key('id').eq(postId)
    )
    
    text = postItem["Items"][0]["originText"]
    voice = postItem["Items"][0]["pollyVoice"]
    timbre = postItem["Items"][0]["pollyTimbre"]
    pitch = postItem["Items"][0]["pollyPitch"]
    rest = text
    
    # Because single invocation of the polly synthesize_speech api can
    # transform text with about 3,000 characters, we are dividing the
    # post into blocks of approximately 2,900 characters.
    textBlocks = []
    while (len(rest) > 3000):
        begin = 0
        end = rest.find(".", 2900)
        if (end == -1):
            end = rest.find(" ", 2900)
        textBlock = rest[begin:end]
        rest = rest[end:]
        textBlocks.append(textBlock)
    textBlocks.append(rest)
     
    #For each block, invoke Polly API, which will transform text into audio
    polly = boto3.client('polly')
    for textBlock in textBlocks:
        removeBrackets = re.sub(r'\([^)]*\)', '', textBlock)
        repTextBlock = re.sub('[·…]', '<break time="100ms"/>', removeBrackets)
        #repTextBlock = re.sub('["·\'…]', '<break time="100ms"/>', removeBrackets)
        ssmlBlock = "<speak><amazon:effect vocal-tract-length=\"" + timbre + "\"><prosody pitch=\"" + pitch + "\">" + repTextBlock + "</prosody></amazon:effect></speak>"
        #print (ssmlBlock)
        response = polly.synthesize_speech(OutputFormat='mp3', Text = ssmlBlock, VoiceId = voice, TextType = 'ssml')
    
        #Save the audio stream returned by Amazon Polly on Lambda's temp
        # directory. If there are multiple text blocks, the audio stream
        # will be combined into a single file.
        if "AudioStream" in response:
            with closing(response["AudioStream"]) as stream:
                output = os.path.join("/tmp/", postId)
                with open(output, "a") as file:
                    file.write(stream.read())
                    
    s3 = boto3.client('s3')
    s3.upload_file('/tmp/' + postId,
        os.environ['BUCKET_NAME'],
        postId + ".mp3")
    s3.put_object_acl(ACL='public-read',
        Bucket=os.environ['BUCKET_NAME'],
        Key= postId + ".mp3")
            
    location = s3.get_bucket_location(Bucket=os.environ['BUCKET_NAME'])
    region = location['LocationConstraint']
    
    if region is None:
        url_begining = "https://s3.amazonaws.com/"
    else:
        url_begining = "https://s3-" + str(region) + ".amazonaws.com/" \
        
    url = url_begining \
        + str(os.environ['BUCKET_NAME']) \
        + "/" \
        + str(postId) \
        + ".mp3"

    #Updating the item in DynamoDB
    response = table.update_item(
        Key={'id':postId},
            UpdateExpression=
                "SET #statusAtt = :statusValue, #urlAtt = :urlValue",
            ExpressionAttributeValues=
                {':statusValue': 'UPDATED', ':urlValue': url},
            ExpressionAttributeNames=
                {'#statusAtt': 'pollyStatus', '#urlAtt': 'mp3Url'},
    )
        
    return


코드 블럭
languageyml
themeRDark
titleCoverAudio Template
linenumberstrue
  ConvertAudio:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: ConvertAudio
      Description: Convert Audio using Amazon Polly
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:Query'
                - 'dynamodb:UpdateItem'
                - 's3:GetBucketLocation'
                - 's3:PutObject'
                - 's3:PutObjectAcl'
                - 'polly:SynthesizeSpeech'
              Resource: '*'
      Events:
        ConvertResource:
          Type: SNS
          Properties:
            Topic:
              Ref: NewsTopic



코드 블럭
languagepy
themeRDark
titleGetNews
linenumberstrue
from __future__ import print_function

import boto3
import os
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr

# https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

def lambda_handler(event, context):
    if "queryStringParameters" in event:
        event = event['queryStringParameters']
    print (event)
    
    postId = event["postId"]
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['DB_TABLE_NAME'])
 
    if postId == "*":
        items = table.scan()
    else:
        items = table.query(KeyConditionExpression=Key('id').eq(postId))
        
    response = {
        'statusCode': 200,
        'body': json.dumps(items["Items"], cls=DecimalEncoder),
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        }
    }

    return response



코드 블럭
languageyml
themeRDark
titleGetNews Template
linenumberstrue
  GetNews:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: GetNews
      Description: Gather information from Ajax calls from web pages
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:Query'
                - 'dynamodb:Scan'
              Resource: '*'
      Events:
        GetNewsApi:
          Type: Api
          Properties:
            Path: /news
            Method: GET


코드 블럭
languagepy
themeRDark
titleDeleteNews
linenumberstrue
from __future__ import print_function

import boto3
import os
import json
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):
    if "body" in event:
        event = json.loads(event['body'])
    print (event)
    
    # Bad Request
    if event["postId"] is None or event["postId"] == "":
        response = {
            'statusCode': 400,
            'body': json.dumps({'message': "An unknown error has occurred. Missing required parameters."}),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }
        return response
    
    postId = event["postId"]
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['DB_TABLE_NAME'])
    table.delete_item(Key={"id":postId})
    
    s3 = boto3.client('s3')
    s3.delete_object(Bucket=os.environ['BUCKET_NAME'], Key= postId + ".mp3")

    response = {
        'statusCode': 200,
        'body': json.dumps({'message': "item is deleted : " + postId}),
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        }
    }
    
    return response


코드 블럭
languageyml
themeRDark
titleDeleteNews Template
linenumberstrue
  DeleteNews:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: DeleteNews
      Description: Delete news item in DynamoDB Table and mp3 file in S3 bucket.
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 'logs:PutLogEvents'
                - 'logs:CreateLogStream'
                - 'dynamodb:DeleteItem'
                - 's3:DeleteObject'
              Resource: '*'
      Events:
        DeleteNewsApi:
          Type: Api
          Properties:
            Path: /news
            Method: DELETE







SAM(template.yml)에 DynamoDB, SNS, S3(Web, Mp3) 리소스 추가하기

...