@@ -15,10 +15,10 @@ import querystring from 'querystring';
1515import config from 'config' ;
1616import urlencode from 'urlencode' ;
1717import elasticsearch from 'elasticsearch' ;
18+ import AWS from 'aws-sdk' ;
1819// import jp from 'jsonpath';
1920import Promise from 'bluebird' ;
2021import models from './models' ;
21- // import AWS from 'aws-sdk';
2222
2323import {
2424 ADMIN_ROLES ,
@@ -30,7 +30,6 @@ import {
3030 RESOURCES ,
3131} from './constants' ;
3232
33- const exec = require ( 'child_process' ) . exec ;
3433const tcCoreLibAuth = require ( 'tc-core-library-js' ) . auth ;
3534
3635const m2m = tcCoreLibAuth . m2m ( config ) ;
@@ -269,28 +268,48 @@ _.assignIn(util, {
269268
270269 /**
271270 * Moves file from source to destination
272- * @param {object } req request object
273- * @param {object } source source object
274- * @param {string } dest destination url
271+ * @param {object } req request object
272+ * @param {string } sourceBucket source bucket
273+ * @param {string } sourceKey source key
274+ * @param {string } destBucket destination bucket
275+ * @param {string } destKey destination key
275276 * @return {promise } promise
276277 */
277- s3FileTransfer : ( req , source , dest ) => new Promise ( ( resolve , reject ) => {
278- const cmdStr = _ . join ( [
279- 'aws s3 mv' ,
280- `"${ source } "` ,
281- `"${ dest } "` ,
282- '--region us-east-1' ,
283- ] , ' ' ) ;
284- exec ( cmdStr , ( error , stdout , stderr ) => {
285- req . log . debug ( `s3FileTransfer: stdout: ${ stdout } ` ) ;
286- req . log . debug ( `s3FileTransfer: stderr: ${ stderr } ` ) ;
287- if ( error !== null ) {
288- req . log . error ( `exec error: ${ error } ` ) ;
289- return reject ( error ) ;
290- }
291- return resolve ( { success : true } ) ;
278+ s3FileTransfer : async ( req , sourceBucket , sourceKey , destBucket , destKey ) => {
279+ const s3 = new AWS . S3 ( {
280+ Region : 'us-east-1' ,
281+ apiVersion : '2006-03-01' ,
292282 } ) ;
293- } ) ,
283+
284+
285+ try {
286+ const sourceParam = {
287+ Bucket : sourceBucket ,
288+ Key : sourceKey ,
289+ } ;
290+
291+ const destParam = {
292+ Bucket : destBucket ,
293+ Key : destKey ,
294+
295+ } ;
296+
297+ const data = await s3 . getObject ( sourceParam ) . promise ( ) ;
298+ req . log . debug ( `s3FileTransfer: getObject successfully: ${ sourceBucket } /${ sourceKey } ` ) ;
299+ destParam . Body = data . Body ;
300+
301+ await s3 . putObject ( destParam ) . promise ( ) ;
302+ req . log . debug ( `s3FileTransfer: putObject successfully: ${ destBucket } /${ destKey } ` ) ;
303+
304+ await s3 . deleteObject ( sourceParam ) . promise ( ) ;
305+ req . log . debug ( `s3FileTransfer: deleteObject successfully: ${ sourceBucket } /${ sourceKey } ` ) ;
306+
307+ return { success : true } ;
308+ } catch ( e ) {
309+ req . log . debug ( `s3FileTransfer: error: ${ e . message } ` ) ;
310+ throw e ;
311+ }
312+ } ,
294313
295314
296315 /**
0 commit comments