Node.js :: Berlioz Documentation

Node.js

Initialize

Install Berlioz SDK:

$ npm install --save berlioz-sdk berlioz-gcp

Include Berlioz SDK:

const berlioz = require('berlioz-sdk');
berlioz.addon(require('berlioz-gcp'));

Accessing Storage Bucket

berlioz.database('<storage-name>').client('storage')

Wraps GCP Storage sdk and return instance of Bucket. All operations return Promise.

Examples

Listing Files

berlioz.database('images').client('storage').getFiles({ prefix: 'hello/'})
    .then(files => {
    })

Downloading File

berlioz.database('images').client('storage').file('photo.png')
    .then(file => {
        return file.createReadStream();
    })
    .then(stream => {
        stream.on('end', () => {
            response.end();
        });
        stream.on('error', (error) => {
            console.log(error);
            response.end();
        });
        stream.pipe(response);
    })

Uploading File

new Promise((resolve, reject) => {
    berlioz.database('images').client('storage').file('photo.png')
        .then(file => {
            return file.createWriteStream();
        })
        .then(writeStream => {
            stream.pipe(writeStream)
                .on('error', (error) => {
                    reject(error);
                })
                .on('finish', () => {
                    resolve();
                });
        })
        .catch(reason => {
            reject(reason);
        });
});