From 89bfa7bd005dfadc5078094e9e0efb53ee7360a4 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Sat, 22 Aug 2020 20:05:54 +0200 Subject: [PATCH] adding option for signed GS URLs --- package.json | 2 +- src/createUri.js | 2 ++ src/createUrl.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 1 + 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/createUrl.js diff --git a/package.json b/package.json index ef99871..3b7de03 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/createUri.js b/src/createUri.js index 6b2c14d..316201a 100644 --- a/src/createUri.js +++ b/src/createUri.js @@ -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('/'); }, } diff --git a/src/createUrl.js b/src/createUrl.js new file mode 100644 index 0000000..bc8ce2b --- /dev/null +++ b/src/createUrl.js @@ -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); + } +}; diff --git a/src/index.js b/src/index.js index 413aec1..4ae61e2 100644 --- a/src/index.js +++ b/src/index.js @@ -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');