-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.js
128 lines (106 loc) · 2.74 KB
/
move.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable func-names */
/*
node-storage-wrapper
*/
module.exports = async function (sourceUri, destinationUri, keepOriginal, logPrefix) {
const thisLogPrefix = logPrefix ? [logPrefix, '>'] : []
let structure, bucket, path
if (sourceUri.substr(0, 5).toLowerCase() === 'gs://' && destinationUri.substr(0, 5).toLowerCase() === 'gs://') {
// google to google transfer
// parse source
structure = sourceUri.substr(5).split('/')
bucket = structure.shift()
path = structure.join('/')
// move file within gcs
if (keepOriginal !== true) {
// move is productive/ destructive
this.sdk.log(
this,
'log',
thisLogPrefix.concat(['storage.move.gcp2gcp >', sourceUri, destinationUri])
)
// move file
await this.sdk.gs.bucket(bucket).file(path).move(destinationUri)
} else {
// mode is dev, only copy file
this.sdk.log(
this,
'log',
thisLogPrefix.concat([
'storage.move.gcp2gcp (only copying) >',
sourceUri,
destinationUri,
])
)
// copy file
await this.sdk.gs.bucket(bucket).file(path).copy(destinationUri)
}
// return ok
return Promise.resolve()
}
if (sourceUri.substr(0, 5).toLowerCase() === 's3://' && destinationUri.substr(0, 5).toLowerCase() === 's3://') {
// google to google transfer
// parse source
structure = destinationUri.substr(5).split('/')
bucket = structure.shift()
path = structure.join('/')
// always copying
this.sdk.log(
this,
'log',
thisLogPrefix.concat(['storage.move.aws2aws copying >', sourceUri, destinationUri])
)
// copy file
await this.sdk.s3
.copyObject({
Bucket: bucket,
Key: path,
CopySource: sourceUri.replace('s3://', '/'),
})
.promise()
// move file within gcs
if (keepOriginal !== true) {
// move is productive/ destructive
this.sdk.log(
this,
'log',
thisLogPrefix.concat(['storage.move.aws2aws deleting source >', sourceUri])
)
// parse source
structure = sourceUri.substr(5).split('/')
bucket = structure.shift()
path = structure.join('/')
// move file
await this.sdk.s3
.deleteObject({
Bucket: bucket,
Key: path,
})
.promise()
}
// return ok
return Promise.resolve()
}
// any to any transfer
this.sdk.log(
this,
'log',
thisLogPrefix.concat(['storage.move.any2any >', keepOriginal, sourceUri, destinationUri])
)
// download file
const blob = await this.load(sourceUri)
// save file to destination
await this.save(destinationUri, blob)
// delete file if in production
if (keepOriginal !== true) {
await this.delete(sourceUri)
} else {
this.sdk.log(
this,
'log',
thisLogPrefix.concat(['storage.move.any2any not deleting sourceUri >', keepOriginal, sourceUri])
)
}
// return ok
return Promise.resolve()
}