This package is intended for use in GCP Cloud Functions and to write and read JavaScript objects as JSON files to/from Cloud Storage.
npm install gcp-object-storage
JavaScript
const gcpObjectStorage = new require('gcp-object-storage');
const objectReader = new gcpObjectStorage.ObjectReader();
const objectWriter = new gcpObjectStorage.ObjectWriter();
const options = {
contentType: 'application/x-font-ttf',
metadata: {
my: 'custom',
properties: 'go here'
},
public: true
};
// providing optional metadata
objectWriter.writeObject({ any: 'data' }, 'my-gcp-bucket', 'folder/to/my/file.json', options);
// without metadata
objectWriter.writeObject({ any: 'data' }, 'my-gcp-bucket', 'folder/to/my/file.json');
objectReader.readObject('my-gcp-bucket', 'folder/to/my/file.json');
// Object { any: "data" }
TypeScript
import { ObjectWriter, ObjectReader, IWriteOptions } from 'gcp-object-storage';
const options: IWriteOptions = {
contentType: 'application/x-font-ttf',
metadata: {
my: 'custom',
properties: 'go here'
},
public: true
};
new ObjectWriter()
.writeObject({ any: 'data' }, 'my-gcp-bucket', 'folder/to/my/file.json', options)
.then(() => new ObjectReader().readObject('my-gcp-bucket', 'folder/to/my/file.json'))
.then((data: any) => console.log(data));
// Object { any: "data" }
npm run test