google-cloud-bucket
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.1.0"></a> | ||
# [0.1.0](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.0.2...v0.1.0) (2018-11-16) | ||
### Features | ||
* Add support for retrieving object from a bucket + update API ([855d52f](https://github.com/nicolasdao/google-cloud-bucket/commit/855d52f)) | ||
<a name="0.0.2"></a> | ||
@@ -7,0 +17,0 @@ ## [0.0.2](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.0.1...v0.0.2) (2018-11-11) |
77
index.js
@@ -15,2 +15,4 @@ /** | ||
const BUCKET_UPLOAD_URL = (bucket, fileName) => `https://www.googleapis.com/upload/storage/v1/b/${encodeURIComponent(bucket)}/o?uploadType=media&name=${encodeURIComponent(fileName)}` | ||
const BUCKET_URL = bucket => `https://www.googleapis.com/storage/v1/b/${encodeURIComponent(bucket)}` | ||
const BUCKET_FILE_URL = (bucket, filepath) => `${BUCKET_URL(bucket)}/o${ filepath ? `${filepath ? `/${encodeURIComponent(filepath)}` : ''}` : ''}` | ||
@@ -30,13 +32,34 @@ const _validateRequiredParams = (params={}) => Object.keys(params).forEach(p => { | ||
Authorization: `Bearer ${token}` | ||
}, content).then(({ status, data }) => { | ||
if (status > 299) { | ||
const message = ((data || {}).error || {}).message || JSON.stringify(data || {}) | ||
let e = new Error(message) | ||
e.code = status | ||
throw e | ||
} | ||
return { status, data } | ||
}, content) | ||
}) | ||
const _getBucketFile = (bucket, filepath, token) => Promise.resolve(null).then(() => { | ||
_validateRequiredParams({ filepath, token }) | ||
return fetch.get(`${BUCKET_FILE_URL(bucket, filepath)}?alt=media`, { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}` | ||
}) | ||
}) | ||
const _retryFn = (fn, options={}) => retry( | ||
fn, | ||
() => true, | ||
{ ignoreFailure: true, retryInterval: 800 }) | ||
.catch(e => { | ||
if (options.retryCatch) | ||
return options.retryCatch(e) | ||
else | ||
throw e | ||
}) | ||
.then(({ status, data }) => { | ||
if (status > 299) { | ||
const message = ((data || {}).error || {}).message || JSON.stringify(data || {}) | ||
let e = new Error(message) | ||
e.code = status | ||
throw e | ||
} | ||
return { status, data } | ||
}) | ||
const createClient = ({ jsonKeyFile }) => { | ||
@@ -51,28 +74,30 @@ _validateRequiredParams({ jsonKeyFile }) | ||
const putObject = (object, filePath) => getToken(auth).then(token => _putObject(object, filePath, token)) | ||
const getObject = (bucket, filePath) => getToken(auth).then(token => _getBucketFile(bucket, filePath, token)) | ||
const retryPutObject = (object, filePath, options={}) => retry( | ||
() => putObject(object, filePath), | ||
() => true, | ||
err => { | ||
if (err && err.message && err.message.indexOf('access') > 0) | ||
return false | ||
else | ||
return true | ||
}, | ||
{ ignoreFailure: true, retryInterval: 800 }) | ||
.catch(e => { | ||
if (options.retryCatch) | ||
return options.retryCatch(e) | ||
else | ||
throw e | ||
}) | ||
const retryPutObject = (object, filePath, options={}) => _retryFn(() => putObject(object, filePath), options) | ||
const retryGetObject = (filePath, options={}) => Promise.resolve(null).then(() => { | ||
if (!filePath) | ||
throw new Error(`Missing required argument 'filePath'`) | ||
const [bucket, ...rest] = filePath.replace(/^\//, '').split('/') | ||
const file = rest.join('/') | ||
if (!file) | ||
throw new Error(`Invalid filePath '${filePath}'. 'filePath' must describe a file (e.g., 'your-bucket/some-optional-path/your-file.json'). It seems you've only passed a bucket.`) | ||
return _retryFn(() => getObject(bucket, file), options) | ||
}) | ||
return { | ||
putObject: retryPutObject | ||
insert: retryPutObject, | ||
'get': retryGetObject | ||
} | ||
} | ||
module.exports = createClient | ||
module.exports = { | ||
client: { | ||
new: createClient | ||
} | ||
} | ||
{ | ||
"name": "google-cloud-bucket", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Nodejs package to add objects to a Google Cloud Bucket.", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"lint": "eslint index.js src/ test/ --fix", | ||
"push": "git push --follow-tags origin master", | ||
"push": "git push --follow-tags origin master && npm publish", | ||
"rls": "standard-version --release-as", | ||
@@ -29,0 +29,0 @@ "test": "mocha", |
@@ -38,6 +38,6 @@ # 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) | ||
const { join } = require('path') | ||
const createClient = require('google-cloud-bucket') | ||
const { client } = require('google-cloud-bucket') | ||
const client = createClient({ | ||
jsonKeyFile: join(__dirname, './service-account.json') | ||
const storage = client.new({ | ||
jsonKeyFile: join(__dirname, './service-account.json') | ||
}) | ||
@@ -52,3 +52,5 @@ | ||
client.putObject(someObject, 'your-bucket/a-path/filename.json').then(res => console.log(res)) | ||
storage.insert(someObject, 'your-bucket/a-path/filename.json') // insert an object into a bucket 'a-path/filename.json' does not need to exist | ||
.then(() => storage.get('your-bucket/a-path/filename.json')) // retrieve that new object | ||
.then(res => console.log(JSON.stringify(res, null, ' '))) | ||
``` | ||
@@ -55,0 +57,0 @@ |
@@ -10,12 +10,12 @@ /** | ||
const postData = (url, headers={}, body) => Promise.resolve(null).then(() => { | ||
return fetch(url, { method: 'POST', headers, body }) | ||
.then(res => res.json().then(data => ({ status: res.status, data }))) | ||
}) | ||
const _processResponse = res => res.json() | ||
.then(data => ({ status: res.status, data })) | ||
.catch(() => ({ status: 200, data: res })) | ||
const getData = (url, headers={}) => Promise.resolve(null).then(() => { | ||
return fetch(url, { method: 'GET', headers }) | ||
.then(res => res.json().then(data => ({ status: res.status, data }))) | ||
}) | ||
const postData = (url, headers={}, body) => | ||
fetch(url, { method: 'POST', headers, body }).then(_processResponse) | ||
const getData = (url, headers={}) => | ||
fetch(url, { method: 'GET', headers }).then(_processResponse) | ||
module.exports = { | ||
@@ -22,0 +22,0 @@ post: postData, |
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
26150
501
94
4