alice
alice

Reputation: 11

How to Upload a File to Google Cloud Storage in Nestjs?

I was searching on the internet for a tutorial on how to upload a file in google storage with nestjs , but I cant find anything. Someone can help me ?

-how to connect google cloud with nestjs server?

-how to upload a file from client side in google cloud with nestjs?

Upvotes: 1

Views: 9460

Answers (2)

Dominic
Dominic

Reputation: 3473

You can use like-fs with like-fs-gcs. It's a library for accessing Google Cloud Storage using an api very similar to the native fs package.

It has support to be used as a NestJS module.

The dependency on NestJS has been removed, so it can be used without NestJS as well. Though you can easily wrap it in a module like this:

import { Module } from '@nestjs/common';
import { GCSFilesystem } from 'like-fs-gcs';
import { TmpFilesystem, LocalFilesystem } from 'like-fs';
import { getStorage } from 'firebase-admin/storage';

@Module({
    providers: [
        {
            provide: GCSFilesystem,
            useFactory() {
                const bucket = getStorage().bucket();
                return new GCSFilesystem(bucket);
            },
        },
        {
            provide: TmpFilesystem,
            useValue: new TmpFilesystem({
                tmpDirectory: 'molzait',
            }),
        },
        LocalFilesystem,
    ],
    exports: [
        { provide: GCSFilesystem, useExisting: GCSFilesystem },
        { provide: TmpFilesystem, useExisting: TmpFilesystem },
    ],
})
export class FilesystemModule {}

The class GCSFilesystem can be instantiated with either to bucket from firebase-admin (getStorage().bucket()) or with a bucket from @google-cloud/storage (new Storage().bucket(process.env.GCS_BUCKET))


@Module({
  imports: [
    FilesystemModule
  ],
  providers: [
    SomeService
  ]
})
export class MyAppModule {

}
import {createReadStream} from 'fs';

@Injectable()
export class SomeService {

  constructor(private readonly fs: GCSFilesystem) {}

  uploadFile(localFile: string) {
    return this.fs.writeStreamToFile('some/path', createReadStream(localFile))
  }

}

Disclosure: I'm the maintainer of this project

Upvotes: 1

Wisnu
Wisnu

Reputation: 337

You can follow this URL https://www.npmjs.com/package/@google-cloud/storage.

There you can find many sample code for using GCP, such as sample code for uploading files which can be found here https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadFile.js.

For implementation in NestJS, because NestJS supports dependency injection, we create a provider first.

export const GCP_PRODUCT_PHOTO_STORAGE_BUCKET = 'GCP_PRODUCT_PHOTO_STORAGE_BUCKET';

export const gcpProductPhotoStorageProvider: Provider<StorageContract> = 
{
  provide: GCP_PRODUCT_PHOTO_STORAGE_BUCKET,
  useFactory: (configService: ConfigService) => {
    const bucketName = process.env.GOOGLE_PRODUCT_PHOTO_BUCKET;
    const baseUrl = `https://${bucketName}`;

    const bucket = new Storage().bucket(bucketName);
    return new GoogleStorageService(bucket, baseUrl);
  },
  inject: [ConfigService],
};

After we register the provider to the module, then we can immediately use it like this on services.

import { Bucket } from '@google-cloud/storage';

@Injectable()
export class GoogleStorageService{
  constructor(private readonly bucket: Bucket, private readonly baseUrl: string) {}

  async upload(
    directory: string,
    image: {
      name: string;
      buffer: Buffer;
    },
    metadata?: object,
  ): Promise<string> {
    const filePath = path.join(directory, image.name);
    const file = this.bucket.file(filePath);

    const options = metadata ? { metadata } : undefined;

    await file.save(image.buffer, options);
    const uri = new URL(this.baseUrl);
    if (this.baseUrl.endsWith(this.bucket.name)) {
      uri.pathname = path.join(directory, image.name);
    } else {
      uri.pathname = path.join(this.bucket.name, directory, image.name);
    }
    return uri.toString();
  }
}

Maybe you can't use this answer immediately, but at least it can give you an idea of how GCP works in NestJS.

Upvotes: 1

Related Questions