@@ -15,10 +15,10 @@ import querystring from 'querystring';
15
15
import config from 'config' ;
16
16
import urlencode from 'urlencode' ;
17
17
import elasticsearch from 'elasticsearch' ;
18
+ import AWS from 'aws-sdk' ;
18
19
// import jp from 'jsonpath';
19
20
import Promise from 'bluebird' ;
20
21
import models from './models' ;
21
- // import AWS from 'aws-sdk';
22
22
23
23
import {
24
24
ADMIN_ROLES ,
@@ -30,7 +30,6 @@ import {
30
30
RESOURCES ,
31
31
} from './constants' ;
32
32
33
- const exec = require ( 'child_process' ) . exec ;
34
33
const tcCoreLibAuth = require ( 'tc-core-library-js' ) . auth ;
35
34
36
35
const m2m = tcCoreLibAuth . m2m ( config ) ;
@@ -269,28 +268,49 @@ _.assignIn(util, {
269
268
270
269
/**
271
270
* 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
275
276
* @return {promise } promise
276
277
*/
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' ,
292
282
} ) ;
293
- } ) ,
283
+
284
+
285
+ try {
286
+ const sourceParam = {
287
+ Bucket : sourceBucket ,
288
+ Key : sourceKey ,
289
+ } ;
290
+
291
+ const copyParam = {
292
+ Bucket : destBucket ,
293
+ Key : destKey ,
294
+ CopySource : `${ sourceBucket } /${ sourceKey } ` ,
295
+ } ;
296
+
297
+ await s3 . copyObject ( copyParam ) . promise ( ) ;
298
+ req . log . debug ( `s3FileTransfer: copyObject successfully: ${ sourceBucket } /${ sourceKey } ` ) ;
299
+ // expect delteObject not block the request
300
+ setTimeout ( async ( ) => {
301
+ try {
302
+ await s3 . deleteObject ( sourceParam ) . promise ( ) ;
303
+ req . log . debug ( `s3FileTransfer: deleteObject successfully: ${ sourceBucket } /${ sourceKey } ` ) ;
304
+ } catch ( e ) {
305
+ req . log . debug ( `s3FileTransfer: deleteObject failed: ${ sourceBucket } /${ sourceKey } : ${ e . message } ` ) ;
306
+ }
307
+ } ) ;
308
+ return { success : true } ;
309
+ } catch ( e ) {
310
+ req . log . debug ( `s3FileTransfer: error: ${ e . message } ` ) ;
311
+ throw e ;
312
+ }
313
+ } ,
294
314
295
315
296
316
/**
0 commit comments