Create Client#

Let create a s3 client

import {
GetObjectCommand,
ListObjectsCommand,
PutObjectCommand,
S3Client
} from '@aws-sdk/client-s3'
import { PollyClient, SynthesizeSpeechCommand } from '@aws-sdk/client-polly'
import * as fs from 'fs'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import { sign } from 'crypto'
const s3Client = new S3Client({
region: 'us-east-1'
})

List Object#

Let list objects in a bucket

const listObject = async ({ bucketName }: { bucketName: string }) => {
try {
const res = await s3Client.send(
new ListObjectsCommand({
Bucket: bucketName
})
)
console.log(res)
} catch (error) {
console.log(error)
}
}
listObject({ bucketName: 'cdk-entest-videos' })

Get Object#

Let download an object from s3 bucket

interface Book {
bucketName: string
key: string
}
const getObjects = async (book: Book) => {
console.log(book.bucketName, book.key)
}
// getObjects({ bucketName: "cdk-entest-videos", key: "song/hello.mp3" });
// signed url s3 object
const get_signed_url = async () => {
const command = new GetObjectCommand({
Bucket: 'cdk-entest-videos',
Key: 'evaluation_1.png'
})
const url = await getSignedUrl(s3Client as any, command as any, {
expiresIn: 3600
})
console.log(url)
}

Signed Url#

Generate signed url for both upload and download

const get_signed_url = async () => {
const command = new GetObjectCommand({
Bucket: 'cdk-entest-videos',
Key: 'evaluation_1.png'
})
const url = await getSignedUrl(s3Client as any, command as any, {
expiresIn: 3600
})
console.log(url)
}
// get_signed_url();
// signed url for upload
const getSignedUrlUpload = async () => {
const command = new PutObjectCommand({
Bucket: 'cdk-entest-videos',
Key: 'pirf/hehehe.jpg'
})
const url = await getSignedUrl(s3Client as any, command as any)
console.log(url)
}

Polly Client#

Let create a polly client to generate audio and save to s3

const pollyClient = new PollyClient({
region: 'ap-southeast-1'
})
const synthesizeSpeed = async ({ message }: { message: string }) => {
const response = await pollyClient.send(
new SynthesizeSpeechCommand({
Engine: 'standard',
LanguageCode: 'en-US',
OutputFormat: 'mp3',
Text: message,
VoiceId: 'Amy'
})
)
// console.log("audio stream ", response.AudioStream);
// save audio stream to mp3 file
const audio = await response.AudioStream?.transformToByteArray()
if (audio?.buffer) {
fs.writeFileSync('hello.mp3', Buffer.from(audio.buffer))
} else {
console.log('error audio buffer')
}
if (response.AudioStream) {
const blob = new Blob([response.AudioStream as Blob])
const url = URL.createObjectURL(blob)
console.log(url)
}
}
// synthesizeSpeed({ message: "Good morning Hai Tran" });

Upload Signed URL#

Let use signed url to upload a file

import axios from 'axios'
const API_DOWNLOAD_URL =
'https://API_GW_ID.execute-api.ap-southeast-1.amazonaws.com/prod/download/'
const API_UPLOAD_URL =
'https://API_GW_ID.execute-api.ap-southeast-1.amazonaws.com/prod/upload/'
// get download signed url
const response = await axios.get(API_DOWNLOAD_URL, {
params: {
key: 'pirf/tree.jpg'
}
})
// console.log(response);
// get upload signed url
const signedUploadUrl = await axios.get(API_UPLOAD_URL, {
params: {
key: 'pirf/hehehe.jpg'
}
})
console.log(signedUploadUrl.data.signedUrl)
// upload by post request
axios.post(signedUploadUrl.data.signedUrl as string, {
headers: {
'Content-Type': 'multipart/form-data'
}
})