google-cloud-bucket
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.2.1"></a> | ||
## [0.2.1](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.2.0...v0.2.1) (2018-12-05) | ||
### Features | ||
* Add bucket specific API ([3072773](https://github.com/nicolasdao/google-cloud-bucket/commit/3072773)) | ||
<a name="0.2.0"></a> | ||
@@ -7,0 +17,0 @@ # [0.2.0](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.1.5...v0.2.0) (2018-11-26) |
19
index.js
@@ -10,2 +10,3 @@ /** | ||
const googleAuth = require('google-auto-auth') | ||
const { posix } = require('path') | ||
const { promise: { retry } } = require('./src/utils') | ||
@@ -64,2 +65,3 @@ const gcp = require('./src/gcp') | ||
const getBucket = (bucket) => getToken(auth).then(token => gcp.config.get(bucket, token)) | ||
const isBucketPublic = (bucket) => getToken(auth).then(token => gcp.config.isBucketPublic(bucket, token)) | ||
const updateConfig = (bucket, config={}) => getToken(auth).then(token => gcp.config.update(bucket, config, token)) | ||
@@ -104,2 +106,19 @@ const addPublicAccess = filePath => getToken(auth).then(token => { | ||
} | ||
}, | ||
bucket: (bucketName) => { | ||
if (!bucketName) | ||
throw new Error('Missing required \'bucketName\' argument') | ||
return { | ||
'get': () => getBucket(bucketName), | ||
update: (config={}) => updateConfig(bucketName, config), | ||
addPublicAccess: () => addPublicAccess(bucketName), | ||
removePublicAccess: () => removePublicAccess(bucketName), | ||
isPublic: () => isBucketPublic(bucketName), | ||
object: { | ||
'get': (filePath, options={}) => retryGetObject(posix.join(bucketName, filePath), options), | ||
insert: (object, filePath, options={}) => retryPutObject(object, posix.join(bucketName, filePath), options), | ||
addPublicAccess: (filePath) => addPublicAccess(posix.join(bucketName, filePath)), | ||
removePublicAccess: (filePath) => removePublicAccess(posix.join(bucketName, filePath)) | ||
} | ||
} | ||
} | ||
@@ -106,0 +125,0 @@ } |
{ | ||
"name": "google-cloud-bucket", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Nodejs package to add objects to a Google Cloud Bucket.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,2 +38,4 @@ # Google Cloud Bucket · [![NPM](https://img.shields.io/npm/v/google-cloud-bucket.svg?style=flat)](https://www.npmjs.com/package/google-cloud-bucket) [![Tests](https://travis-ci.org/nicolasdao/google-cloud-bucket.svg?branch=master)](https://travis-ci.org/nicolasdao/google-cloud-bucket) [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Neap](https://neap.co/img/made_by_neap.svg)](#this-is-what-we-re-up-to) | ||
### Basics | ||
```js | ||
@@ -72,13 +74,47 @@ const { join } = require('path') | ||
.then(({ data:{ uri } }) => console.log(`Your web page is publicly available at: ${uri}`)) | ||
``` | ||
// MAKING AN EXISTING OBJECT READ PUBLIC (warning: Your service account must have the 'roles/storage.objectAdmin' role) | ||
> 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](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. | ||
### 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). | ||
```js | ||
// MAKING AN EXISTING OBJECT PUBLICLY READABLE (warning: Your service account must have the 'roles/storage.objectAdmin' role) | ||
storage.addPublicAccess('your-bucket/a-path/private.html') | ||
.then(({ data:{ uri } }) => console.log(`Your web page is publicly available at: ${uri}`)) | ||
// MAKING A BUCKET READ PUBLIC (warning: Your service account must have the 'roles/storage.admin' role) | ||
// MAKING A BUCKET PUBLICLY READABLE (warning: Your service account must have the 'roles/storage.admin' role) | ||
// Once a bucket is public, all content added to it (even when omitting the 'public' flag) is public | ||
storage.addPublicAccess('your-bucket') | ||
.then(({ data:{ uri } }) => console.log(`Your web page is publicly available at: ${uri}`)) | ||
// Alternatively you can also use this API: | ||
storage.config('your-bucket').addPublicAccess() | ||
.then(({ data:{ uri } }) => console.log(`Your web page is publicly available at: ${uri}`)) | ||
// REMOVE THE PUBLICLY READABLE ACCESS FROM A BUCKET (warning: Your service account must have the 'roles/storage.admin' role) | ||
storage.removePublicAccess('your-bucket') | ||
// or | ||
storage.config('your-bucket').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](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) on your bucket: | ||
```js | ||
// CONFIGURE CORS ON A BUCKET (warning: Your service account must have the 'roles/storage.admin' role) | ||
storage.config('your-bucket').update({ | ||
cors:[{ | ||
origin: ['*'], | ||
method: ['GET', 'OPTIONS', 'HEAD'], | ||
responseHeader: ['Authorization', 'Origin', 'X-Requested-With', 'Content-Type', 'Accept'], | ||
maxAgeSeconds: 3600 | ||
}] | ||
}).then(() => `CORS successfully set up on your bucket.`) | ||
``` | ||
# This Is What We re Up To | ||
@@ -85,0 +121,0 @@ 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](https://neap.co). |
@@ -85,2 +85,17 @@ /** | ||
// Doc: https://cloud.google.com/storage/docs/json_api/v1/ | ||
const isBucketPublic = (bucket, token) => Promise.resolve(null).then(() => { | ||
_validateRequiredParams({ bucket, token }) | ||
return getBucket(bucket, token).then(({ data }) => { | ||
const bindings = data && data.iam ? (data.iam.bindings || []) : [] | ||
const objectViewerBinding = bindings.find(b => b && b.role == 'roles/storage.objectViewer') | ||
if (!objectViewerBinding) | ||
bindings.push({ | ||
role: 'roles/storage.objectViewer', | ||
members: ['allUsers'] | ||
}) | ||
return !objectViewerBinding || (objectViewerBinding.members || []).some(m => m == 'allUsers') | ||
}) | ||
}) | ||
// Doc: https://cloud.google.com/storage/docs/json_api/v1/ | ||
const makePublic = (bucket, filepath, token) => Promise.resolve(null).then(() => { | ||
@@ -124,3 +139,3 @@ _validateRequiredParams({ bucket, token }) | ||
else | ||
return { status: 200, data: { message: 'The IAM binding role \'roles/storage.objectViewer\' for member \'allUsers\' has already been setup.' } } | ||
return { status: 200, data: { message: 'The public access was already added.' } } | ||
@@ -177,3 +192,3 @@ const payload = JSON.stringify({ bindings }) | ||
if (!objectViewerBinding || (objectViewerBinding && !(objectViewerBinding.members || []).some(m => m == 'allUsers'))) | ||
return { status: 200, data: { message: 'The IAM binding role \'roles/storage.objectViewer\' for member \'allUsers\' was never there to begin within.' } } | ||
return { status: 200, data: { message: 'The public access was already removed.' } } | ||
else | ||
@@ -245,3 +260,4 @@ bindings = bindings.map(b => { | ||
'get': getBucket, | ||
update: updateConfig | ||
update: updateConfig, | ||
isBucketPublic | ||
} | ||
@@ -248,0 +264,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47349
949
156