Skip to content

Commit

Permalink
adding option for signed GS URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Aug 22, 2020
1 parent b565496 commit 89bfa7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swrlab/node-storage-wrapper",
"version": "0.0.11",
"version": "0.0.12",
"description": "Wrapping AWS S3, GCP GCS, file storage",
"main": "./src/index.js",
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions src/createUri.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

module.exports = {
s3: (bucket, path) => {
// return data
return ['s3:/', bucket, path].join('/');
},
gs: (bucket, path) => {
// return data
return ['gs:/', bucket, path].join('/');
},
}
54 changes: 54 additions & 0 deletions src/createUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
node-storage-wrapper
This module provides easy access to combine bucket + path to unique URIs
*/

module.exports = async function (uri, ttl) {
try {
if (uri.substr(0, 5).toLowerCase() == 's3://') {
// log progress
this.sdk.log(this, 'log', ['storage.createUrl.aws >', uri]);

return Promise.reject('not implemented');
} else if (uri.substr(0, 5).toLowerCase() == 'gs://') {
// google cloud storage
let structure = uri.substr(5).split('/');
let bucket = structure.shift();
let path = structure.join('/');

// log progress
this.sdk.log(this, 'log', ['storage.createUrl.gcp >', uri]);

// set config
const config = {
action: 'read',
expires: Date.now() + ttl,
};

// create link
var [url] = await this.sdk.gs.bucket(bucket).file(path).getSignedUrl(config);

// return link
return Promise.resolve(url);
} else if (
uri.substr(0, 7).toLowerCase() == 'http://' ||
uri.substr(0, 8).toLowerCase() == 'https://'
) {
// log progress
this.sdk.log(this, 'log', ['storage.createUrl.https >', uri]);

// return link
return Promise.resolve(uri);
} else {
// log progress
this.sdk.log(this, 'log', ['storage.createUrl.local >', uri]);

return Promise.reject('not implemented');
}
} catch (err) {
return Promise.reject(err);
}
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function StorageWrapper(config) {

// import functions
this.createUri = require('./createUri');
this.createUrl = require('./createUrl');
this.delete = require('./delete');
this.list = require('./list');
this.load = require('./load');
Expand Down

0 comments on commit 89bfa7b

Please sign in to comment.