Skip to content

Commit

Permalink
Simplify and modernize code to upload to Google Cloud Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
YoranBrondsema committed Nov 26, 2019
1 parent 4b9f7d1 commit 1e0dd0d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
extends: 'eslint:recommended',
env: {
browser: true
node: true
},
rules: {
}
Expand Down
27 changes: 7 additions & 20 deletions lib/gcloud/storage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* jshint node: true */
'use strict';

const Readable = require('stream').Readable;
const { Storage } = require('@google-cloud/storage');

class GoogleCloudStorage {

constructor(credentials) {
const options = {};
if (credentials) {
Expand All @@ -15,25 +13,14 @@ class GoogleCloudStorage {
this.storage = new Storage(options);
}

upload(bucket, key, file) {
let upload = this.storage.bucket(bucket).file(key);

if (typeof file === 'string') {
let stream = new Readable();
stream.push(file);
stream.push(null);
file = stream;
}

return new Promise(function(resolve, reject){
file.pipe(upload.createWriteStream())
.on('error', function(error){
reject('Failed to upload file ' + key + ' - ' + error.message);
})
.on('finish', resolve);
});
upload(bucket, key, filepath) {
return this.storage.bucket(bucket).upload(
filepath,
{
destination: key
}
);
}

}

module.exports = GoogleCloudStorage;
43 changes: 24 additions & 19 deletions lib/tasks/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const GoogleCloudStorage = require('../gcloud/storage');

const fs = require('fs');
const path = require('path');
const util = require('util');

class GoogleCloudStorageUploadTask {

Expand All @@ -19,34 +20,38 @@ class GoogleCloudStorageUploadTask {
this.storage = new GoogleCloudStorage(options.credentials);
}


run() {
return this.uploadHashedZip()
.then(() => this.uploadVersionFile());
async run() {
await this.uploadHashedZip();
await this.uploadVersionFile();
}

uploadHashedZip() {
let hashedZipPath = this.hashedZipPath;
let bucket = this.bucket;

this.log('uploading ' + hashedZipPath + ' to ' + bucket, { verbose: true });

let file = fs.createReadStream(hashedZipPath);
this.log(`uploading ${this.hashedZipPath} to ${this.bucket} at ${this.hashedZipKey}`, { verbose: true });

return this.storage.upload(bucket, this.hashedZipKey, file);
return this.storage.upload(
this.bucket,
this.hashedZipKey,
path.resolve(this.hashedZipPath)
);
}

uploadVersionFile() {
let versionFile = {
bucket: this.bucket,
key: this.hashedZipKey
};
async uploadVersionFile() {
this.log(`uploading version file to ${this.bucket} at ${this.key}`, { verbose: true });

versionFile = JSON.stringify(versionFile);
const dir = path.dirname(this.hashedZipPath);
const filename = path.resolve(path.join(dir, this.key));

return this.storage.upload(this.bucket, this.key, versionFile);
}
const writeFile = util.promisify(fs.writeFile);
await writeFile(
filename,
JSON.stringify({
bucket: this.bucket,
key: this.hashedZipKey
})
);

return this.storage.upload(this.bucket, this.key, filename);
}
}

module.exports = GoogleCloudStorageUploadTask;

0 comments on commit 1e0dd0d

Please sign in to comment.