Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
a13m committed Nov 29, 2017
0 parents commit 877d193
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Background Cloud Function to be triggered by Cloud Storage.
*
* @param {object} event The Cloud Functions event.
* @param {function} callback The callback function.
*/

'use strict';
const AWS = require('aws-sdk');
const gcloud = require('google-cloud');
const runtimeConfig = require('cloud-functions-runtime-config');

exports.syncGCS = function (event, callback) {
const file = event.data;

if (file.resourceState === 'not_exists') {
console.log(`File ${file.name} deleted.`);
} else if (file.metageneration === '1') {
// metageneration attribute is updated on metadata changes.
// on create value is 1
console.log(`File ${file.name} uploaded.`);

const configName = event.data.bucket;

// Fetch "environment" from Google Runtime Configuration
const awsBucketP = runtimeConfig.getVariable(configName, 'aws-bucket');
const awsAccessKeyP = runtimeConfig.getVariable(configName, 'aws-access-key');
const awsSecretKeyP = runtimeConfig.getVariable(configName, 'aws-secret-key');
const regionP = runtimeConfig.getVariable(configName, 'aws-region');

Promise.all([ awsBucketP, awsAccessKeyP, awsSecretKeyP, regionP ]).then(values => {
AWS.config.credentials = new AWS.Credentials(values[1], values[2]);
var s3 = new AWS.S3({region: values[3] });
console.log(`Access key ${process.env.AWS_ACCESS_KEY_ID} ; target bucket ${values[0]}`);

var bucket = gcloud.storage().bucket(event.data.bucket);
var remoteReadStream = bucket.file(file.name).createReadStream();
var s3obj = new AWS.S3({params: {Bucket: values[0], Key: file.name}});
s3obj.upload({Body: remoteReadStream})
.on('httpUploadProgress', function(evt) { console.log(evt); })
.send(function(err, data) { console.log(err, data) });
}).catch(function(e){console.log(e)});
} else {
console.log(`File ${file.name} metadata updated.`);
}
callback();
};

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "gcs2s3",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"pretest": "npm run lint"
},
"dependencies": {
"google-cloud": "0.56.0",
"aws-sdk": "2.100.0",
"cloud-functions-runtime-config": "0.2.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.16",
"ava": "0.21.0",
"proxyquire": "1.8.0",
"sinon": "3.0.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true
}
}

0 comments on commit 877d193

Please sign in to comment.