NestJS - proper way to copy static (non-imported) files to dist folder

Viewed 3854

I'm building an app that uses google cloud apis (e.g. Google Cloud Storage). In order to authenticate the app with the api I need to pass the path of the keyfile.json file that contains auth credentials to the GCloud Api initialization script. I.e.

const storage = new Storage({
      keyFilename: path.join(__dirname, '../../keyfile.json'),
});

I put the keyfile.json into the NestJS src folder but when I build the app, the file is not copied to dist.

What would be recommended way to handle static files copying to dist in NestJS?

2 Answers

Just declare your directory in the assets array of nest-cli.json:

{
    "collection": "@nestjs/schematics",
    "sourceRoot": "src",
    "compilerOptions": {
        "assets": [
            "resources/*"
        ]
    }
}

Source: https://docs.nestjs.com/cli/monorepo#assets

Just to make things clear here and to add an important point:

  • when you create and deploy your app engine, the app engine default service account will be created and used as the identity of the App Engine service.
  • The App Engine default service account is associated with your Google Cloud project and executes tasks on behalf of your apps running in App Engine.
  • By default, the App Engine default service account has the Editor role in the project.
  • Also, Your App Engine app uses the credentials of the App Engine
    service account by default.
  • And that's how it feels like magically your app is making calls to Google Cloud API without authentication settings

You can find this info here.

Related