forked from pendo-io/gcs-s3-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 877d193
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |