Skip to content

Instantly share code, notes, and snippets.

@nagyadam2092
Created June 2, 2018 07:36
Show Gist options
  • Save nagyadam2092/f9f4e1a72b504e6f47778a27e61b19ed to your computer and use it in GitHub Desktop.
Save nagyadam2092/f9f4e1a72b504e6f47778a27e61b19ed to your computer and use it in GitHub Desktop.
'use strict';
const sinon = require('sinon'),
assert = require('assert'),
copyHandler = require('../index').handler;
describe('copy bucket handler', () => {
const S3 = {
copyObject: sinon.spy()
};
it('copies from XYZ to TARGET_BUCKET/__DATE__/XYZ', () => {
const expectedParams = {
CopySource: 'YOUR_EXPECTED_COPY_SOURCE',
Bucket: 'YOUR_EXPECTED_COPY_BUCKET',
Key: 'YOUR_EXPECTED_KEY'
};
// Here is the trick, we are calling the copyHandler with a 4th argument, which is the spy
copyHandler(event, null, console.log, S3);
assert(S3.copyObject.calledWith(expectedParams));
});
});
'use strict';
const AWS = require('aws-sdk');
const defaultS3 = new AWS.S3({ apiVersion: '2006-03-01' });
exports.handler = (event, context, callback, S3 = defaultS3) => {
const sourcekey = event.fillThisHereWithKeyPath;
const sourceBucket = event.fillThisHereWithBucketPath; // get somewhere from your use case the source bucket / key names
const dest = ''; // define the destination bucket
S3.copyObject({
CopySource: `${srcBucket}/${srcKey}`,
Bucket: destinationBucket,
Key: srcKey
}, (copyErr, copyData) => {
if (copyErr) {
throw `Error: ${copyErr}`;
} else {
console.log('Copied OK');
}
});
// Everything went fine, notify caller.
callback(null, `Copy bucket from ${srcBucket} to ${destinationBucket} done.`);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment