Skip to content

Aginix/nestjs-gcloud-storage

Repository files navigation

NPM Version Package License NPM Downloads

Description

Google Cloud Storage Module for Nest.js Framework

Installation

$ yarn add @aginix/nestjs-gcloud-storage

Examples

Default import

If you have bucket domain name you can config storageBaseUri.

import { Module } from '@nestjs/common';
import { GCloudStorageModule } from '@aginix/nestjs-gcloud-storage';

@Module({
  imports: [
    GCloudStorageModule.withConfig({
      defaultBucketname: 'bucket.aginix.tech',
      storageBaseUri: 'bucket.aginix.tech',
      predefinedAcl: 'private' // Default is publicRead
    })
  ],
})
export class AppModule {}

Default import with asynchonous

import { Module } from '@nestjs/common';
import { GCloudStorageModule } from '@aginix/nestjs-gcloud-storage';
import { ConfigService } from './config.service';
import { ConfigModule } from './config.module';

@Module({
  imports: [
    GCloudStorageModule.withConfigAsync({
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        defaultBucketname: config.get('GCS_BUCKET_NAME'),
        storageBaseUri: config.get('GCS_DOMAIN_NAME'),
      }),
      imports: [ConfigModule],
    })
  ],
})
export class AppModule {}

Using uniform bucket-level access

Set predefinedAcl to null

@Module({
  imports: [
    GCloudStorageModule.withConfig({
      predefinedAcl: null
    })
  ],
})
export class AppModule {}

Store a file using the default config

import {
  Controller,
  Logger,
  Post,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  GCloudStorageFileInterceptor,
  UploadedFileMetadata,
} from '@aginix/nestjs-gcloud-storage';

@Controller()
export class AppController {
  
  @Post('gcs/upload')
  @UseInterceptors(
    GCloudStorageFileInterceptor('file'),
  )
  UploadedFilesUsingInterceptor(
    @UploadedFile()
    file: UploadedFileMetadata,
  ) {
    Logger.log(`Storage URL: ${file.storageUrl}`, 'AppController');
  }
}

Store a file using a specific folder/prefix name

import {
  Controller,
  Logger,
  Post,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  GCloudStorageFileInterceptor,
  UploadedFileMetadata,
} from '@aginix/nestjs-gcloud-storage';

@Controller()
export class AppController {
  
  @Post('gcs/upload')
  @UseInterceptors(
    GCloudStorageFileInterceptor('file', undefined, { prefix: 'prefix/test' })
  )
  UploadedFilesUsingInterceptor(
    @UploadedFile()
    file: UploadedFileMetadata,
  ) {
    Logger.log(`Storage URL: ${file.storageUrl}`, 'AppController');
  }
}

Store a file using a specific predefined ACL

@Controller()
export class AppController {
  
  @Post('gcs/secret-file')
  @UseInterceptors(
    GCloudStorageFileInterceptor('secret-file', undefined, { predefinedAcl: 'private' })
  )
  UploadedSecretFile(
    @UploadedFile()
    file: UploadedFileMetadata,
  ) {
    Logger.log(`Storage URL: ${file.storageUrl}`, 'AppController');
  }
}

License

MIT © Aginix Technologies Co., Ltd.