๐ฏ AWS ๋ด์ ๊ตฌ์ถํ Lambda์์ S3์ ์ ๊ทผํ์ฌ ํ์ผ ์ฝ๊ธฐ
โ Lambda, S3๋ฅผ ํ์ต ํ ์ ์๋ค.
๐ S3 ๋ฒํท ์์ฑ
์๋์ ๊ฐ์ด S3 ๋ฒํท์ ๋ง๋ค์ด๋ณด์.





๐ S3์ ํ์ผ ์ ๋ก๋
๋๋ค์์ ์ธ ๋ฐ์ดํฐ๋ฅผ ์ ๋ก๋ ํด๋ณด์



๐ IAM Role ๋ง๋ค๊ธฐ
Lambda๋ฅผ ์ํ IAM Role์ ๋ง๋ค์ด S3 ์์ธ์ค ๊ถํ์ ๋ถ์ฌํ์






๐ Lambda ํจ์ ์์ฑํ๊ธฐ
์ด์ S3์ ์ฐ๊ฒฐ ํ lambda๋ฅผ ๋ง๋ค์ด๋ณด์.



๐ Lambda ํจ์ ์์ฑํ์ฌ S3 ๋ฐ์ดํฐ ํธ์ถํ๊ธฐ
์๋ ์ํ ์ฝ๋๋ฅผ ์ด์ฉํ์ฌ S3์ ์ฐ๊ฒฐํด๋ณด์.
https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/example_s3_GetObject_section.html
AWS SDK๋ฅผ ์ฌ์ฉํ์ฌ Amazon S3 ๋ฒํท์์ ๊ฐ์ฒด ๊ฐ์ ธ์ค๊ธฐ - Amazon Simple Storage Service
์ด ์ค๋ช ์๋ ํ๊ฐํ ๋ฒ์ SDK์ ๊ดํ ๊ฒ์ ๋๋ค. SDK๋ ๋ณ๊ฒฝ๋ ์ ์์ผ๋ฉฐ ํ๋ก๋์ ์์ ์ฌ์ฉํด์๋ ์ ๋ฉ๋๋ค.
docs.aws.amazon.com
import { GetObjectCommand } from "@aws-sdk/client-s3"; import { S3Client } from "@aws-sdk/client-s3"; const REGION = "ap-northeast-2"; //e.g. "us-east-1" const s3Client = new S3Client({ region: REGION }); export const handler = async(event) => { const bucketParams = { Bucket: "crocus-lamdba-s3-test-bucket", Key: "test.txt" } try { // Create a helper function to convert a ReadableStream to a string. const streamToString = (stream) => new Promise((resolve, reject) => { const chunks = []; stream.on("data", (chunk) => chunks.push(chunk)); stream.on("error", reject); stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8"))); }); // Get the object} from the Amazon S3 bucket. It is returned as a ReadableStream. const data = await s3Client.send(new GetObjectCommand(bucketParams)); // Convert the ReadableStream to a string. const bodyContents = await streamToString(data.Body); return { statusCode: 200, body: JSON.stringify(bodyContents), }; } catch (err) { console.log("Error", err); } };


'Applied > AWS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java์์ aws lambda๋ก internal invoke ์ํค๊ธฐ (0) | 2024.01.20 |
---|---|
nodejs Lambda ํจ์์์ ๋ค๋ฅธ Lambda ํจ์ ํธ์ถํ๊ธฐ (0) | 2023.12.17 |
[AWS] KMS(Key Management Service) ์ค์ต (0) | 2022.11.30 |
Yaml ํ์ผ์ด๋? (0) | 2022.09.20 |
[AWS] Lambda๋? (0) | 2022.09.18 |