From ffc869ee2797e2ae15ef4525b520dc3964ce2cb9 Mon Sep 17 00:00:00 2001 From: Yunus Date: Tue, 30 Apr 2024 18:28:00 +0700 Subject: [PATCH] Decouple s3 --- server/api/artifacts/detail-artifact.get.ts | 2 +- server/api/artifacts/download-artifact.get.ts | 2 +- .../api/artifacts/upload-artifact-url.post.ts | 4 +-- server/api/artifacts/upload-artifact.post.ts | 2 +- server/api/install/download.get.ts | 2 +- server/services/S3AppClient.ts | 27 ++++++++++++++++--- 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/server/api/artifacts/detail-artifact.get.ts b/server/api/artifacts/detail-artifact.get.ts index 2684856..bcf5cd2 100644 --- a/server/api/artifacts/detail-artifact.get.ts +++ b/server/api/artifacts/detail-artifact.get.ts @@ -33,7 +33,7 @@ export default defineEventHandler(async (event) => { const s3 = new S3AppClient() const { assets } = getStorageKeys(userOrg.organizationsId!, app.id, detailArtifact.fileObjectKey) const [headObject, groups] = await Promise.all([ - s3.send(event, new HeadObjectCommand({ + s3.getHeadObject(event, new HeadObjectCommand({ Bucket: s3BucketName, Key: assets, })).then(e => e as AppHeadObjectCommandOutput), diff --git a/server/api/artifacts/download-artifact.get.ts b/server/api/artifacts/download-artifact.get.ts index 7816260..3a2e76b 100644 --- a/server/api/artifacts/download-artifact.get.ts +++ b/server/api/artifacts/download-artifact.get.ts @@ -33,7 +33,7 @@ export default defineEventHandler(async (event) => { }).then(takeUniqueOrThrow) const { assets } = getStorageKeys(userOrg.organizationsId!, app.id, detailArtifact.fileObjectKey) const s3 = new S3AppClient() - const signedUrl = await s3.getSignedUrl(event, new GetObjectCommand({ + const signedUrl = await s3.getSignedUrlGetObject(event, new GetObjectCommand({ Bucket: s3BucketName, Key: assets, ResponseContentDisposition: `attachment; filename ="${app.name}"`, diff --git a/server/api/artifacts/upload-artifact-url.post.ts b/server/api/artifacts/upload-artifact-url.post.ts index 88ccca1..dcd73fc 100644 --- a/server/api/artifacts/upload-artifact-url.post.ts +++ b/server/api/artifacts/upload-artifact-url.post.ts @@ -45,12 +45,12 @@ export default defineEventHandler(async (event) => { }) const { temp, assets } = getStorageKeys(userOrg.organizationsId!, app.id, key) const s3 = new S3AppClient() - await s3.send(event, new CopyObjectCommand({ + await s3.copyObject(event, new CopyObjectCommand({ CopySource: `${s3BucketName}/${temp}`, Bucket: s3BucketName, Key: assets, })) - await s3.send(event, new DeleteObjectCommand({ + await s3.deleteObject(event, new DeleteObjectCommand({ Bucket: s3BucketName, Key: temp, })) diff --git a/server/api/artifacts/upload-artifact.post.ts b/server/api/artifacts/upload-artifact.post.ts index c1fb77a..942054d 100644 --- a/server/api/artifacts/upload-artifact.post.ts +++ b/server/api/artifacts/upload-artifact.post.ts @@ -28,7 +28,7 @@ export default defineEventHandler(async (event) => { var expires = 300; const { temp } = getStorageKeys(userOrg.organizationsId!, app.id, key) const s3 = new S3AppClient() - const signedUrl = await s3.getSignedUrl(event, new PutObjectCommand({ + const signedUrl = await s3.getSignedUrlPutObject(event, new PutObjectCommand({ Bucket: s3BucketName, Key: temp, ContentType: 'application/vnd.android.package-archive', diff --git a/server/api/install/download.get.ts b/server/api/install/download.get.ts index c4b9856..0209b39 100644 --- a/server/api/install/download.get.ts +++ b/server/api/install/download.get.ts @@ -42,7 +42,7 @@ export default defineEventHandler(async (event) => { }).then(takeUniqueOrThrow) const { assets } = getStorageKeys(org.id, app.id, detailArtifact.fileObjectKey) const s3 = new S3AppClient() - const signedUrl = await s3.getSignedUrl(event, new GetObjectCommand({ + const signedUrl = await s3.getSignedUrlGetObject(event, new GetObjectCommand({ Bucket: s3BucketName, Key: assets, ResponseContentDisposition: `attachment; filename ="${app.name}"` diff --git a/server/services/S3AppClient.ts b/server/services/S3AppClient.ts index d82c642..709b5e1 100644 --- a/server/services/S3AppClient.ts +++ b/server/services/S3AppClient.ts @@ -1,6 +1,7 @@ import type { EventHandlerRequest, H3Event } from "h3"; import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; -import { S3Client } from "@aws-sdk/client-s3"; +import { CopyObjectCommand, DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client, type HeadObjectCommandOutput } from "@aws-sdk/client-s3"; +import { ofetch } from "ofetch" const createS3 = (event: H3Event) => { const { S3_ENDPOINT, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY } = useRuntimeConfig(event) @@ -16,7 +17,7 @@ const createS3 = (event: H3Event) => { } export class S3AppClient { - async getSignedUrl(event: H3Event, command: any, expiresIn: number): Promise { + async getSignedUrlPutObject(event: H3Event, command: PutObjectCommand, expiresIn: number): Promise { const s3 = createS3(event) const signedUrl = await getSignedUrl(s3, command, { expiresIn: expiresIn, @@ -24,7 +25,27 @@ export class S3AppClient { return signedUrl } - async send(event: H3Event, command: any): Promise { + async getSignedUrlGetObject(event: H3Event, command: GetObjectCommand, expiresIn: number): Promise { + const s3 = createS3(event) + const signedUrl = await getSignedUrl(s3, command, { + expiresIn: expiresIn, + }) + return signedUrl + } + + async getHeadObject(event: H3Event, command: HeadObjectCommand): Promise { + const s3 = createS3(event) + const result = await s3.send(command) + return result + } + + async copyObject(event: H3Event, command: CopyObjectCommand): Promise { + const s3 = createS3(event) + const result = await s3.send(command) + return result + } + + async deleteObject(event: H3Event, command: DeleteObjectCommand): Promise { const s3 = createS3(event) const result = await s3.send(command) return result