@pittica/google-cloud-storage-helpers 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/storage/file.js +21 -13
- package/package.json +1 -1
package/dist/storage/file.js
CHANGED
|
@@ -16,6 +16,9 @@ const {
|
|
|
16
16
|
Storage
|
|
17
17
|
} = require("@google-cloud/storage");
|
|
18
18
|
const log = require("@pittica/logger-helpers");
|
|
19
|
+
const {
|
|
20
|
+
getStorage
|
|
21
|
+
} = require("./storage");
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* Moves all files from a bucket to another.
|
|
@@ -24,21 +27,26 @@ const log = require("@pittica/logger-helpers");
|
|
|
24
27
|
* @param {string} destination Destination bucket.
|
|
25
28
|
*/
|
|
26
29
|
exports.moveFiles = async (source, destination) => {
|
|
27
|
-
const storage =
|
|
28
|
-
retryOptions: {
|
|
29
|
-
autoRetry: true,
|
|
30
|
-
retryDelayMultiplier: 4
|
|
31
|
-
}
|
|
32
|
-
});
|
|
30
|
+
const storage = getStorage();
|
|
33
31
|
const bucket = storage.bucket(destination);
|
|
34
32
|
const [files] = await storage.bucket(source).getFiles();
|
|
35
|
-
files.forEach(results => results.forEach(file =>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
files.forEach(results => Array.isArray(results) ? results.forEach(file => this.moveFile(file, bucket)) : this.moveFile(results, bucket));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Moves a file from a bucket to another.
|
|
38
|
+
*
|
|
39
|
+
* @param {File} file File object.
|
|
40
|
+
* @param {Bucket} bucket Bucket object.
|
|
41
|
+
*/
|
|
42
|
+
exports.moveFile = async (file, bucket) => {
|
|
43
|
+
const {
|
|
44
|
+
id
|
|
45
|
+
} = file;
|
|
46
|
+
const source = file.metadata.bucket;
|
|
47
|
+
const destination = bucket.id;
|
|
48
|
+
log.info(`Moving "${id}" from "${source}" to "${destination}"`);
|
|
49
|
+
file.copy(bucket.file(id)).then(() => this.deleteFile(file) ? log.success(`Moved "${id}" from "${source}" to "${destination}"`) : log.error(`Failed deleted "${id}" in "${source}"`)).catch(() => log.error(`Failed moving "${id}" from "${source}" to "${destination}"`));
|
|
42
50
|
};
|
|
43
51
|
|
|
44
52
|
/**
|