@tzmedical/fastify-google-cloud-storage
v1.0.0
Published
A fastify plugin for Google cloud storage integrations
Downloads
136
Readme
Fastify Plugin for Google Cloud Storage
A plugin for Fastify to use the Google Cloud Storage Client for publishing and subscribing to topics.
Install
npm install @tzmedical/fastify-google-cloud-storageBasic Usage
After registering the plugin, the storage instance is added to a cloudStorage decorator on the fastify object. The options object on the register call is passed to the storage instance when it is constructed.
See Google Cloud Storage - Using the client library for more information.
Example with Application Default Credentials
const cloudStorageClient = require('@tzmedical/fastify-google-cloud-storage')
module.exports = async function(fastify, opts) {
// Register the client as a plugin
fastify.register(cloudStorageClient)
// Upload a file
await fastify.cloudStorage.bucket('bucket-name').file('new-file').save('file-contents')
// Move a file
await fastify.cloudStorage.bucket('bucket-name').file('old-file').move('new-location/old-file')
}Example with key file path
const cloudStorageClient = require('@tzmedical/fastify-google-cloud-storage')
module.exports = async function(fastify, opts) {
// Register the client as a plugin with path to credentials
fastify.register(
cloudStorageClient,
{ projectId: 'your-project-id', keyFilename: '/path/to/keyfile.json' }
)
}Example with inline credentials
const cloudStorageClient = require('@tzmedical/fastify-google-cloud-storage')
module.exports = async function(fastify, opts) {
// Register the client as a plugin with path to credentials
fastify.register(
cloudStorageClient,
{
projectId: 'your-project-id',
credentials: {
type: 'service_account',
project_id: 'xxxxxxx',
private_key_id: 'xxxx',
private_key:'-----BEGIN PRIVATE KEY-----xxxxxxx\n-----END PRIVATE KEY-----\n',
client_email: 'xxxx',
client_id: 'xxx',
auth_uri: 'https://accounts.google.com/o/oauth2/auth',
token_uri: 'https://oauth2.googleapis.com/token',
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
client_x509_cert_url: 'xxx',
}
}
)
}