Google Cloud Bucket ·
![Neap](https://neap.co/img/made_by_neap.svg)
Google Cloud Bucket is node.js package to add objects to a Google Cloud Bucket.
Table of Contents
Install
npm i google-cloud-bucket --save
How To Use It
Prerequisite
Before using this package, you must first:
-
Have a Google Cloud Account.
-
Have a bucket in that Google Account.
-
Have a Service Account set up with the following 2 roles:
roles/storage.objectCreator
roles/storage.objectAdmin
(only if you want to update access to object)roles/storage.admin
(only if you want to update access to an entire bucket)
-
Get the JSON keys file for that Service Account above
-
Save that JSON key into a service-account.json
file. Make sure it is located under a path that is accessible to your app (the root folder usually).
Show Me The Code
Basics
const { join } = require('path')
const { client } = require('google-cloud-bucket')
const storage = client.new({
jsonKeyFile: join(__dirname, './service-account.json')
})
const someObject = {
firstname: 'Nicolas',
lastname: 'Dao',
company: 'Neap Pty Ltd',
city: 'Sydney'
}
storage.insert(someObject, 'your-bucket/a-path/filename.json')
.then(() => storage.get('your-bucket/a-path/filename.json'))
.then(res => console.log(JSON.stringify(res, null, ' ')))
const html = `
<!doctype html>
<html>
<body>
<h1>Hello Giiiiirls</h1>
</body>
</html>`
storage.insert(html, 'your-bucket/a-path/index.html', { public: true })
.then(({ publicUri }) => console.log(`Your web page is publicly available at: ${publicUri}`))
storage.insert(imgBuffer, 'your-bucket/a-path/image.jpg')
storage.get('your-bucket/a-path/filename.json').then(obj => console.log(obj))
storage.get('your-bucket/a-path/index.html').then(htmlString => console.log(htmlString))
storage.get('your-bucket/a-path/image.jpg').then(imgBuffer => console.log(imgBuffer))
storage.get('your-bucket/a-path/image.jpg', { dst: 'some-path/image.jpg' }).then(() => console.log(`Image successfully downloaded.`))
const { Writable } = require('stream')
const customReader = new Writable({
write(chunk, encoding, callback) {
console.log('Hello chunk of image')
callback()
}
})
storage.get('your-bucket/a-path/image.jpg', { streamReader: customReader }).then(() => console.log(`Image successfully downloaded.`))
Notice the usage of the public: true
flag in the insert
statement above. This automatically makes this specific file publicly available at https://storage.googleapis.com/your-bucket/a-path/index.html. If that file is supposed to stay private, then don't use that flag. To make the entire bucket public, refer to the next section Publicly Readable Config.
Bucket API
The examples above show how to insert and query any storage. We've also included a variant of those APIs that are more focused on a specific bucket:
storage.insert(someObject, 'your-bucket/a-path/filename.json')
storage.bucket('your-bucket').object('a-path/filename.json').insert(someObject)
storage.get('your-bucket/a-path/filename.json').then(obj => console.log(obj))
storage.bucket('your-bucket').object('a-path/filename.json').get().then(obj => console.log(obj))
Buckets & Files Configuration
Publicly Readable Config
This allows to make any files publicly readable by anybody on the web. That's usefull if you want to host a website, or publish data (e.g., RSS feed).
const bucket = storage.bucket('your-bucket')
bucket.isPublic().then(isPublic => isPublic ? console.log(`Bucket '${bucket.name}' is public`) : console.log(`Bucket '${bucket.name}' is not public`))
bucket.addPublicAccess()
.then(({ publicUri }) => console.log(`Your web page is publicly available at: ${publicUri}`))
bucket.removePublicAccess()
bucket.object('a-path/private.html').addPublicAccess()
.then(({ publicUri }) => console.log(`Your web page is publicly available at: ${publicUri}`))
bucket.object('a-path/private.html').removePublicAccess()
Configuring CORS On a Bucket
If your files are publicly readable on the web, they might not be accessible when referenced from other websites. To enable other websites to access your files, you will have to configure CORS on your bucket:
bucket.cors.setup({
origin: ['*'],
method: ['GET', 'OPTIONS', 'HEAD', 'POST'],
responseHeader: ['Authorization', 'Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
maxAgeSeconds: 3600
})
.then(() => console.log(`CORS successfully set up on your bucket.`))
If you want to check if CORS has already been set up on a bucket:
bucket.cors.exists().then(yes => yes
? console.log(`CORS already set up on bucket '${bucket.name}'.`)
: console.log(`CORS not set up yet on bucket '${bucket.name}'.`))
You can also check if a specific CORS config exists:
bucket.cors.exists({
origin: ['*'],
method: ['GET', 'OPTIONS', 'HEAD', 'POST'],
responseHeader: ['Authorization', 'Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
maxAgeSeconds: 3600
}).then(yes => yes
? console.log(`CORS already set up on bucket '${bucket.name}'.`)
: console.log(`CORS not set up yet on bucket '${bucket.name}'.`))
To remove CORS from a bucket:
bucket.cors.disable().then(() => console.log(`CORS successfully disabled on bucket '${bucket.name}'.`))
This Is What We re Up To
We are Neap, an Australian Technology consultancy powering the startup ecosystem in Sydney. We simply love building Tech and also meeting new people, so don't hesitate to connect with us at https://neap.co.
Our other open-sourced projects:
GraphQL
- graphql-serverless: GraphQL (incl. a GraphiQL interface) middleware for webfunc.
- schemaglue: Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
- graphql-s2s: Add GraphQL Schema support for type inheritance, generic typing, metadata decoration. Transpile the enriched GraphQL string schema into the standard string schema understood by graphql.js and the Apollo server client.
- graphql-authorize: Authorization middleware for graphql-serverless. Add inline authorization straight into your GraphQl schema to restrict access to certain fields based on your user's rights.
React & React Native
Tools
License
Copyright (c) 2018, Neap Pty Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Neap Pty Ltd nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL NEAP PTY LTD BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
![Neap Neap Pty Ltd logo](https://neap.co/img/neap_color_horizontal.png)