Skip to content

Commit

Permalink
fix: actually works (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgollucci authored Oct 28, 2024
1 parent 7f5d110 commit d1250d1
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 37 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
"typescript.updateImportsOnFileMove.enabled": "always",
"[yaml]": {
"editor.defaultFormatter": "redhat.vscode-yaml"
}
},
"cSpell.words": [
"presigner"
]
}
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as process from 'node:process'
import esbuild from 'esbuild'

esbuild.build({
entryPoints: ['./src/lambda.ts'],
entryPoints: ['./src/presign.ts'],
bundle: true,
platform: 'node',
target: ['node20'],
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "p6-
"description": "APIGW Receiver Serverless Image Brander for LDAR Pets",
"name": "p6-template-sam-eslint-pnpm-ts-flatfile",
"description": "DESC",
"repository": {
"type": "git",
"url": "https://github.com/luckydoganimalrescue/p6-ldar-cdk-pnp-brand-image.git"
"url": "https://github.com/p6m7g8/p6-template-sam-eslint-pnpm-ts-flatfile.git"
},
"version": "0.3.0",
"version": "0.0.0",
"author": {
"name": "Philip M. Gollucci",
"email": "pgollucci@p6m7g8.com",
Expand All @@ -22,23 +22,23 @@
},
"license": "Apache-2.0",
"type": "module",
"main": "src/lambda.ts",
"main": "src/presign.ts",
"scripts": {
"all": "yarn nuke && yarn install && yarn docker && yarn sam:container && yarn run ci",
"bundle": "node --trace-deprecation esbuild.config.mjs",
"ci": "yarn run lint && yarn run test && yarn run bundle",
"docker": "docker run -it --rm -v $(pwd):/app -w /app node:20 bash -c \"rm -rf node_modules && COREPACK_ENABLE_DOWNLOAD_PROMPT=0 CI=1 corepack enable && COREPACK_ENABLE_DOWNLOAD_PROMPT=0 CI=1 yarn install\"",
"nuke": "rm -rf .aws-sam coverage dist node_modules .yarn",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"sam:container": "sam build --use-container",
"sam:start:api": "yarn run ci && sam local start-api --port 3001 --debug",
"sam:invoke:X": "yarn run ci && sam local invoke XFunction --event events/brand.json",
"sam:api:start": "yarn run ci && sam local start-api --port 3001 --debug",
"sam:presign:invoke": "yarn run ci && sam local invoke PresignFunction --event events/event.json",
"test": "node --trace-deprecation ./node_modules/jest/bin/jest.js --config jest.config.ts --coverage --passWithNoTests",
"test:watch": "node --trace-deprecation ./node_modules/jest/bin/jest.js --config jest.config.ts --watch --coverage",
"type": "tsc --noEmit"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.679.0",
"@aws-sdk/s3-request-presigner": "^3.679.0",
"aws-lambda": "^1.0.7"
},
"devDependencies": {
Expand All @@ -58,4 +58,4 @@
"typescript": "^5.6.3"
},
"packageManager": "yarn@4.5.1"
}
}
5 changes: 5 additions & 0 deletions samconfig.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version = 0.1

[default.deploy]
stack_name = "p6-template-sam-eslint-pnpm-ts-flatfile"
region = "us-east-1"
capabilities = "CAPABILITY_IAM"

[default.package]
s3_bucket = "p6-DNE-CHANGE-ME"
output_template_file = "template-output.yaml"
19 changes: 0 additions & 19 deletions src/lambda.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/presign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {
APIGatewayProxyEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
Context,
} from 'aws-lambda'
import * as process from 'node:process'
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'

export const handler: APIGatewayProxyHandler = async (
event: APIGatewayProxyEvent,
_context: Context,
): Promise<APIGatewayProxyResult> => {
const bucketName = process.env.BUCKET_NAME ?? 'p6-dne'

const client = new S3Client({ region: 'us-east-1' })

const body = JSON.parse(event.body || '{]')
const filename = body.filename

const command = new PutObjectCommand({
Bucket: bucketName,
Key: filename,
})

const url = await getSignedUrl(client, command, { expiresIn: 3600 })

return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
body: JSON.stringify({ url }),
}
}
16 changes: 11 additions & 5 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@ Resources:
AllowHeaders: "'Content-Type'"
AllowOrigin: "'*'"

XFunction:
PresignFunction:
Type: AWS::Serverless::Function
Properties:
Handler: dist/X.handler
Handler: dist/presign.handler
Runtime: nodejs20.x
CodeUri: ./dist
Policies:
- S3FullAccessPolicy:
BucketName: !Sub "${BUCKET_NAME}"
Environment:
Variables:
BUCKET_NAME: !Sub "${BUCKET_NAME}"
Events:
ApiEvent:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /X
Path: /presign
Method: post

XunctionLogGroup:
PresignFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/lambda/XFunctionLogGroup
LogGroupName: /aws/lambda/PresignFunctionLogGroup
RetentionInDays: 14
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"include": [
"jest.config.ts",
"src/**/*.ts",
"src/**/*.tsx",
"test/**/*.ts",
],
"exclude": [
"dist",
"node_modules"
]
}
}

0 comments on commit d1250d1

Please sign in to comment.