Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Package] NestJS GCS Client #358

Closed
TorinAsakura opened this issue Mar 15, 2025 · 1 comment · Fixed by #359
Closed

[Package] NestJS GCS Client #358

TorinAsakura opened this issue Mar 15, 2025 · 1 comment · Fixed by #359
Assignees
Labels
feature New feature or request
Milestone

Comments

@TorinAsakura
Copy link
Member

Название пакета

@atls/nestjs-gcs-client

Для чего нужен пакет?

Интеграциюя с GCS максимально прозрачной и удобной в DI NestJS

  • GcsClientModule — NestJS-модуль с register и registerAsync
  • GcsClientConfigFactory — фабрика для конфигурации клиента
  • GcsClientFactory — создаёт инстанс Storage
  • Поддержка DI через useFactory, useClass, useExisting

Пример использования

Пример

import { Module } from '@nestjs/common'
import { GcsClientModule } from '@atls/nestjs-gcs-client'

@Module({
  imports: [
    GcsClientModule.register({
      keyFilename: 'path/to/key.json',
      apiEndpoint: 'https://storage.googleapis.com',
    }),
  ],
})
export class AppModule {}

Асинхронная регистрация

import { Module } from '@nestjs/common'
import { GcsClientModule } from '@atls/nestjs-gcs-client''
import { ConfigModule, ConfigService } from '@nestjs/config'

@Module({
  imports: [
    ConfigModule.forRoot(),
    GcsClientModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        keyFilename: configService.get<string>('GCS_KEY_FILENAME'),
        apiEndpoint: configService.get<string>('GCS_API_ENDPOINT'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Использование в сервисе

import { Injectable } from '@nestjs/common'
import { GcsClientFactory } from '@atls/nestjs-gcs-client''

@Injectable()
export class StorageService {
  private storage

  constructor(private readonly gcsFactory: GcsClientFactory) {
    this.storage = gcsFactory.create()
  }

  async uploadFile(bucketName: string, filePath: string, destination: string) {
    const bucket = this.storage.bucket(bucketName)
    await bucket.upload(filePath, { destination })
  }
}

Материалы

No response

@TorinAsakura TorinAsakura added this to the NestJS v2 milestone Mar 15, 2025
@TorinAsakura TorinAsakura self-assigned this Mar 15, 2025
@Amiditin
Copy link
Member

Пакет опубликован https://www.npmjs.com/package/@atls/nestjs-gcs-client

@Amiditin Amiditin linked a pull request Mar 15, 2025 that will close this issue
@Amiditin Amiditin added the feature New feature or request label Mar 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants