Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: S3バケット名の変更と新規バケット追加 #260

Merged
merged 4 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backend/lambda/bouquet_create/bouquet_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
dynamodb = boto3.resource("dynamodb")
s3 = boto3.client("s3")

FLOWER_BUCKET_NAME = os.environ["FLOWER_BUCKET_NAME"]
ORIGINAL_IMAGE_BUCKET_NAME = os.environ["ORIGINAL_IMAGE_BUCKET_NAME"]
mt-osho-san marked this conversation as resolved.
Show resolved Hide resolved
BOUQUET_BUCKET_NAME = os.environ["BOUQUET_BUCKET_NAME"]
GENERATIVE_AI_TABLE_NAME = os.environ["GENERATIVE_AI_TABLE_NAME"]
BOUQUET_TABLE_NAME = os.environ["BOUQUET_TABLE_NAME"]
Expand Down Expand Up @@ -847,7 +847,7 @@ def load_image(self, key):
Returns:
Image: ロードされた画像
"""
obj = s3.get_object(Bucket=FLOWER_BUCKET_NAME, Key=key)
obj = s3.get_object(Bucket=ORIGINAL_IMAGE_BUCKET_NAME, Key=key)
img = Image.open(obj["Body"]).convert("RGBA")
return img

Expand Down
2 changes: 1 addition & 1 deletion src/backend/lambda/diary_create/diary_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_img_from_s3(flower_id):
Exception: S3 操作が失敗した場合に発生。
"""
s3 = boto3.client("s3")
bucket_name = os.environ["FLOWER_IMAGE_BUCKET_NAME"]
bucket_name = os.environ["ORIGINAL_IMAGE_BUCKET_NAME"]
s3_key = f"flowers/{flower_id}.png"

try:
Expand Down
2 changes: 1 addition & 1 deletion src/backend/lambda/flower_get/flower_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_img_from_s3(flower_id: str) -> str:
str: 画像のバイナリデータ(Base64エンコード済み)
"""
s3 = boto3.client("s3")
bucket_name = os.environ["BUCKET_NAME"]
bucket_name = os.environ["FLOWER_BUCKET_NAME"]
s3_key = f"flowers/{flower_id}.png"

try:
Expand Down
5 changes: 3 additions & 2 deletions src/backend/lib/backend-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ export class BackendStack extends cdk.Stack {
})
// Diary機能コンストラクトのスタック化
const diary = new Diary(this, 'Diary', {
flowerBucket: flower.flowerBucket,
mt-osho-san marked this conversation as resolved.
Show resolved Hide resolved
userPool: auth.userPool,
api: api.api,
cognitoAuthorizer: api.cognitoAuthorizer,
table: flower.table,
generativeAiTable: flower.generativeAiTable,
flowerSelectFunction: flower.flowerSelectFunction,
flowerImageBucket: flower.flowerImageBucket,
originalImageBucket: flower.originalImageBucket,
})

const bouquet = new Bouquet(this, 'Bouquet', {
Expand All @@ -68,7 +69,7 @@ export class BackendStack extends cdk.Stack {
api: api.api,
generativeAiTable: flower.generativeAiTable,
cognitoAuthorizer: api.cognitoAuthorizer,
flowerImageBucket: flower.flowerImageBucket,
originalImageBucket: flower.originalImageBucket,
})

const settings = new Settings(this, 'Settings', {
Expand Down
6 changes: 3 additions & 3 deletions src/backend/lib/constructs/bouquet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface BouquetProps {
api: apigateway.RestApi
generativeAiTable: dynamodb.Table
cognitoAuthorizer: apigateway.CognitoUserPoolsAuthorizer
flowerImageBucket: s3.Bucket
originalImageBucket: s3.Bucket
}

export class Bouquet extends Construct {
Expand Down Expand Up @@ -66,13 +66,13 @@ export class Bouquet extends Construct {
environment: {
GENERATIVE_AI_TABLE_NAME: props.generativeAiTable.tableName,
BOUQUET_TABLE_NAME: bouquetTable.tableName,
FLOWER_BUCKET_NAME: props.flowerImageBucket.bucketName,
ORIGINAL_IMAGE_BUCKET_NAME: props.originalImageBucket.bucketName,
BOUQUET_BUCKET_NAME: bouquetBucket.bucketName,
},
})
props.generativeAiTable.grantReadData(BouquetCreate)
bouquetTable.grantWriteData(BouquetCreate)
props.flowerImageBucket.grantRead(BouquetCreate)
props.originalImageBucket.grantRead(BouquetCreate)
bouquetBucket.grantPut(BouquetCreate)

// /bouquet APIの設定
Expand Down
9 changes: 6 additions & 3 deletions src/backend/lib/constructs/diary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface DiaryProps {
table: dynamodb.Table
generativeAiTable: dynamodb.Table
flowerSelectFunction: lambda.Function
flowerImageBucket: s3.Bucket
originalImageBucket: s3.Bucket
flowerBucket: s3.Bucket
}

export class Diary extends Construct {
Expand All @@ -36,14 +37,16 @@ export class Diary extends Construct {
logRetention: 14,
environment: {
TABLE_NAME: props.table.tableName,
FLOWER_IMAGE_BUCKET_NAME: props.flowerImageBucket.bucketName,
ORIGINAL_IMAGE_BUCKET_NAME: props.originalImageBucket.bucketName,
FLOWER_SELECT_FUNCTION_NAME: props.flowerSelectFunction.functionName,
FLOWER_BUKCET_NAME: props.flowerBucket.bucketName,
},
timeout: cdk.Duration.seconds(30),
})
props.table.grantWriteData(diaryCreateFunction)
props.flowerSelectFunction.grantInvoke(diaryCreateFunction)
props.flowerImageBucket.grantRead(diaryCreateFunction)
props.originalImageBucket.grantRead(diaryCreateFunction)
props.flowerBucket.grantPut(diaryCreateFunction)

// 日記編集用Lambda関数の定義
const diaryEditFunction = new lambda.Function(this, 'diaryEditLambda', {
Expand Down
27 changes: 19 additions & 8 deletions src/backend/lib/constructs/flower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export interface FlowerProps {
}

export class Flower extends Construct {
public readonly flowerImageBucket: s3.Bucket
public readonly originalImageBucket: s3.Bucket
public readonly table: dynamodb.Table
public readonly generativeAiTable: dynamodb.Table
public readonly flowerSelectFunction: lambda.Function
public readonly flowerBucket: s3.Bucket
constructor(scope: Construct, id: string, props: FlowerProps) {
super(scope, id)

Expand Down Expand Up @@ -51,8 +52,14 @@ export class Flower extends Construct {
pointInTimeRecovery: true,
})

// 花の画像保存用S3バケットの作成
const flowerImageBucket = new s3.Bucket(this, 'flowerImageBucket', {
// 元画像保存用S3バケットの作成
const originalImageBucket = new s3.Bucket(this, 'originalImageBucket', {
enforceSSL: true,
serverAccessLogsPrefix: 'log/',
})

// ユーザーごとの花画像保存用S3バケットの作成
const flowerBucket = new s3.Bucket(this, 'flowerBucket', {
enforceSSL: true,
serverAccessLogsPrefix: 'log/',
})
Expand All @@ -70,13 +77,15 @@ export class Flower extends Construct {
environment: {
DIARY_TABLE_NAME: table.tableName,
GENERATIVE_AI_TABLE_NAME: generativeAiTable.tableName,
FLOWER_BUCKET_NAME: flowerImageBucket.bucketName,
ORIGINAL_IMAGE_BUCKET_NAME: originalImageBucket.bucketName,
FLOWER_BUCKET_NAME: flowerBucket.bucketName,
},
timeout: cdk.Duration.seconds(60),
})
generativeAiTable.grantWriteData(flowerSelectFunction)
flowerBucket.grantPut(flowerSelectFunction)
table.grantStreamRead(flowerSelectFunction)
flowerImageBucket.grantPut(flowerSelectFunction)
originalImageBucket.grantPut(flowerSelectFunction)
const difyApiKey = ssm.StringParameter.fromStringParameterAttributes(this, 'DifyApiKey', {
parameterName: 'DIFY_API_KEY',
})
Expand All @@ -93,12 +102,13 @@ export class Flower extends Construct {
handler: 'flower_get.lambda_handler',
code: lambda.Code.fromAsset('lambda/flower_get'),
environment: {
BUCKET_NAME: flowerImageBucket.bucketName,
ORIGINAL_BUCKET_NAME: originalImageBucket.bucketName,
GENERATIVE_AI_TABLE_NAME: generativeAiTable.tableName,
FLOWER_BUCKET_NAME: flowerBucket.bucketName,
},
timeout: cdk.Duration.seconds(10),
})
flowerImageBucket.grantRead(flowerGetFunction)
originalImageBucket.grantRead(flowerGetFunction)
generativeAiTable.grantReadData(flowerGetFunction)
// flower API の設定
const flowerApi = props.api.root.addResource('flower')
Expand Down Expand Up @@ -138,9 +148,10 @@ export class Flower extends Construct {
authorizer: props.cognitoAuthorizer,
})

this.flowerImageBucket = flowerImageBucket
this.originalImageBucket = originalImageBucket
this.table = table
this.generativeAiTable = generativeAiTable
this.flowerSelectFunction = flowerSelectFunction
this.flowerBucket = flowerBucket
}
}
Loading
Loading