-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save assets and generate presigned URLs
- Loading branch information
1 parent
87a335b
commit cc2d10b
Showing
9 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
MigrationInterface, | ||
QueryRunner, | ||
Table, | ||
} from "typeorm" | ||
|
||
export class CreateAsset1697978452568 implements MigrationInterface { | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: "asset", | ||
columns: [ | ||
{ | ||
name: "id", | ||
type: "int", | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: 'increment', | ||
}, | ||
{ | ||
name: "filename", | ||
type: "varchar", | ||
}, | ||
{ | ||
name: "uid", | ||
type: "varchar", | ||
}, | ||
], | ||
}), | ||
true, | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropTable("asset") | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateAssetDto } from './dto/create-asset.dto'; | ||
import { EntityManager, Repository } from 'typeorm'; | ||
import { Asset } from './entities/asset.entity'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner' | ||
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; | ||
import { ConfigService } from "@nestjs/config"; | ||
|
||
@Injectable() | ||
export class AssetsService { | ||
constructor( | ||
@InjectRepository(Asset) | ||
private readonly assetsRepository: Repository<Asset>, | ||
private readonly entityManager: EntityManager, | ||
private readonly configService: ConfigService, | ||
) {} | ||
|
||
async create(createAssetDto: CreateAssetDto) { | ||
const asset = new Asset({ | ||
...createAssetDto, | ||
}); | ||
await this.entityManager.save(asset); | ||
} | ||
|
||
async findOne(id: number) { | ||
const asset = await this.assetsRepository.findOne({ | ||
where: { id }, | ||
}); | ||
|
||
const s3 = new S3Client({ | ||
region: this.configService.getOrThrow('AWS_REGION'), | ||
credentials: { | ||
accessKeyId: this.configService.getOrThrow('AWS_ACCESS_KEY_ID'), | ||
secretAccessKey: this.configService.getOrThrow('AWS_SECRET_KEY_ID'), | ||
}, | ||
}); | ||
|
||
const bucketName = this.configService.getOrThrow('AWS_BUCKET'); | ||
const objectKey = asset.uid; | ||
const expirationInSeconds = 3600; | ||
|
||
// Generate the pre-signed URL | ||
const command = new GetObjectCommand({ | ||
Bucket: bucketName, | ||
Key: objectKey, | ||
}); | ||
|
||
const url = await getSignedUrl(s3, command, { expiresIn: expirationInSeconds }); | ||
|
||
return { url, filename: asset.filename }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class CreateAssetDto { | ||
filename: string; | ||
uid: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class Asset { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
filename: string; | ||
|
||
@Column() | ||
uid: string; | ||
|
||
constructor(asset: Partial<Asset>) { | ||
Object.assign(this, asset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters