Skip to content

Commit

Permalink
Decouple s3
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed Apr 30, 2024
1 parent 72e687d commit ffc869e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/api/artifacts/detail-artifact.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion server/api/artifacts/download-artifact.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`,
Expand Down
4 changes: 2 additions & 2 deletions server/api/artifacts/upload-artifact-url.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))
Expand Down
2 changes: 1 addition & 1 deletion server/api/artifacts/upload-artifact.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion server/api/install/download.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`
Expand Down
27 changes: 24 additions & 3 deletions server/services/S3AppClient.ts
Original file line number Diff line number Diff line change
@@ -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<EventHandlerRequest>) => {
const { S3_ENDPOINT, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY } = useRuntimeConfig(event)
Expand All @@ -16,15 +17,35 @@ const createS3 = (event: H3Event<EventHandlerRequest>) => {
}

export class S3AppClient {
async getSignedUrl(event: H3Event<EventHandlerRequest>, command: any, expiresIn: number): Promise<string> {
async getSignedUrlPutObject(event: H3Event<EventHandlerRequest>, command: PutObjectCommand, expiresIn: number): Promise<string> {
const s3 = createS3(event)
const signedUrl = await getSignedUrl(s3, command, {
expiresIn: expiresIn,
})
return signedUrl
}

async send(event: H3Event<EventHandlerRequest>, command: any): Promise<any> {
async getSignedUrlGetObject(event: H3Event<EventHandlerRequest>, command: GetObjectCommand, expiresIn: number): Promise<string> {
const s3 = createS3(event)
const signedUrl = await getSignedUrl(s3, command, {
expiresIn: expiresIn,
})
return signedUrl
}

async getHeadObject(event: H3Event<EventHandlerRequest>, command: HeadObjectCommand): Promise<any> {
const s3 = createS3(event)
const result = await s3.send(command)
return result
}

async copyObject(event: H3Event<EventHandlerRequest>, command: CopyObjectCommand): Promise<any> {
const s3 = createS3(event)
const result = await s3.send(command)
return result
}

async deleteObject(event: H3Event<EventHandlerRequest>, command: DeleteObjectCommand): Promise<any> {
const s3 = createS3(event)
const result = await s3.send(command)
return result
Expand Down

0 comments on commit ffc869e

Please sign in to comment.