Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Open Handler for Google Cloud Storage / gcs #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement Open Handler for Google Cloud Storage / gcs
  • Loading branch information
tobiasstrebitzer committed Jan 4, 2022
commit c96e382b2c11e0a5713f096b3fb79a152f140608
15 changes: 15 additions & 0 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ module.exports = {
return directory(source, options);
},

gcs : function(client, params, options) {
const bucket = client.bucket(params.Bucket);
const file = bucket.file(params.Key);
var source = {
size: function() {
return file.getMetadata().then(([response]) => +response.size)
},
stream: function(offset,length) {
return file.createReadStream({ start: offset, end: length ? length : undefined })
}
};

return directory(source, options);
},

custom: function(source, options) {
return directory(source, options);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"setimmediate": "~1.0.4"
},
"devDependencies": {
"@google-cloud/storage": "^5.16.1",
"aws-sdk": "^2.77.0",
"dirdiff": ">= 0.0.1 < 1",
"iconv-lite": "^0.4.24",
Expand Down
21 changes: 21 additions & 0 deletions test/openGcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var test = require('tap').test;
var fs = require('fs');
var path = require('path');
var unzip = require('../');
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();

test("get content of a single file entry out of a zip", function (t) {
return unzip.Open.gcs(storage, { Bucket: 'unzipper', Key: 'archive.zip' }).then(function(d) {
var file = d.files.filter(function(file) {
return file.path == 'content.opf';
})[0];
return file.buffer().then(function(str) {
var fileStr = fs.readFileSync(path.join(__dirname, '../testData/compressed-standard/inflated/file.txt'), 'utf8');
t.equal(str.toString(), fileStr);
t.end();
});
});
});