Feat: repo api done
This commit is contained in:
@@ -1,8 +1,41 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Readable } from 'stream';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
S3Client,
|
||||
S3ClientConfig,
|
||||
GetObjectCommand,
|
||||
GetObjectCommandInput,
|
||||
} from '@aws-sdk/client-s3';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
private readonly s3Client: S3Client;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
this.s3Client = new S3Client({
|
||||
region: 'us-west-1',
|
||||
endpoint: this.configService.get<string>('MINIO_ENDPOINT'),
|
||||
credentials: {
|
||||
accessKeyId: this.configService.get<string>('MINIO_ACCESSKEY'),
|
||||
secretAccessKey: this.configService.get<string>('MINIO_SECRETKEY'),
|
||||
},
|
||||
forcePathStyle: true,
|
||||
} as S3ClientConfig);
|
||||
}
|
||||
|
||||
getHealthz(): string {
|
||||
return 'OK';
|
||||
}
|
||||
|
||||
async getObject(getObjectConfig: GetObjectCommandInput): Promise<Readable> {
|
||||
const command = new GetObjectCommand(getObjectConfig);
|
||||
const res = await this.s3Client.send(command);
|
||||
if (!res.Body)
|
||||
throw new HttpException(
|
||||
's3 get object failed',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
return res.Body as Readable;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user