Today I learned: How to upload a buffer as a file to a Google Cloud Storage bucket
Posted on
Context
Today I needed to upload some file to a Google Cloud Storage bucket.
Many of examples out there are illustrated with scenarios using a file already
stored on the file system or coming from some POST
request.
None of these where matching my use case. I needed to upload a JSON file I would
build from a JSON object.
I had no intent to persist this file onto the server's file system, therefore
Node.JS Buffer
would be a great pick.
Solution
First off, you need the @google-cloud/storage npm
module, which is the
Node.js client for the upload Job. So go ahead and run npm install @google-cloud/storage
at the root of your project.
As stated in Google APIs documentation, before you begin:
- Select or create a Cloud Platform project.
- Enable billing for your project.
- Enable the Google Cloud Storage API.
- Set up authentication with a service account so you can access the API from your local workstation.
On top of this I would add Create a bucket to be used as the uploads destination storage.
Now it's coding time...
In the following example I turn a JSON object into a String before uploading, but you could go straight with the String.
const {Storage} = require('@google-cloud/storage');
const character = {
firstName: 'Harry',
lastName: 'Potter'
};
// Turns the object into a String and then a buffer.
const fileContent = JSON.stringify(character, null, 4);
const fileBuffer = Buffer.from(fileContent, 'utf-8');
// Creates a client.
const storage = new Storage({
projectId: 'your-project-id',
keyFilename: 'file-path-to-your-google-service-account-key'
});
const bucket = storage.bucket('name-of-the-upload-bucket'));
const file = bucket.file('name-of-the-destination-file');
// Uploads the file.
file.save(fileBuffer).then(() => {
// Success handling...
}).catch(error => {
// Error handling...
});
In case you need to programmatically check if the file content stored is as you
expect it. In the then
block you can "download" the file to console log its
content as demonstrated below:
// Uploads the file.
file.save(fileBuffer).then(aync () => {
const checkedFile = bucket.file('name-of-the-destination-file');
const content = await checkedFile.download();
console.log(`File content: ${content}`);
}).catch(error => {
// Error handling...
});
That's all folks!