@pittica/google-cloud-storage-helpers 1.1.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/index.js +10 -2
- package/dist/storage/file.js +47 -14
- package/dist/storage/storage.js +29 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
const {
|
|
16
16
|
copyFile,
|
|
17
17
|
deleteFile,
|
|
18
|
-
moveFiles
|
|
18
|
+
moveFiles,
|
|
19
|
+
getFiles,
|
|
20
|
+
copiedFile
|
|
19
21
|
} = require("./storage/file");
|
|
20
22
|
const {
|
|
21
23
|
downloadBucket,
|
|
@@ -26,11 +28,17 @@ const {
|
|
|
26
28
|
writeJson,
|
|
27
29
|
groupJson
|
|
28
30
|
} = require("./storage/json");
|
|
31
|
+
const {
|
|
32
|
+
getStorage
|
|
33
|
+
} = require("./storage/storage");
|
|
29
34
|
exports.copyFile = copyFile;
|
|
30
35
|
exports.deleteFile = deleteFile;
|
|
31
36
|
exports.moveFiles = moveFiles;
|
|
37
|
+
exports.getFiles = getFiles;
|
|
38
|
+
exports.copiedFile = copiedFile;
|
|
32
39
|
exports.writeJson = writeJson;
|
|
33
40
|
exports.groupJson = groupJson;
|
|
34
41
|
exports.downloadFile = downloadFile;
|
|
35
42
|
exports.downloadBucket = downloadBucket;
|
|
36
|
-
exports.downloadFolder = downloadFolder;
|
|
43
|
+
exports.downloadFolder = downloadFolder;
|
|
44
|
+
exports.getStorage = getStorage;
|
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
|
/**
|
|
@@ -59,4 +67,29 @@ exports.copyFile = (source, destination) => source.copy(source.storage.bucket(de
|
|
|
59
67
|
exports.deleteFile = file => file.delete().then(response => {
|
|
60
68
|
const [res] = response;
|
|
61
69
|
return res.statusCode >= 200 && res.statusCode < 300;
|
|
62
|
-
}).catch(() => false);
|
|
70
|
+
}).catch(() => false);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gets the files from the given bucket.
|
|
74
|
+
*
|
|
75
|
+
* @param {Storage} storage Google Cloud Storage object.
|
|
76
|
+
* @param {string} bucket Bucket name.
|
|
77
|
+
* @param {string} prefix Files prefix.
|
|
78
|
+
* @returns {Array} Files from the given bucket.
|
|
79
|
+
*/
|
|
80
|
+
exports.getFiles = (storage, source, prefix) => storage.bucket(source).getFiles({
|
|
81
|
+
prefix
|
|
82
|
+
}).then(files => files).catch(() => []);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Process the response from copy API and extracts the response file object.
|
|
86
|
+
*
|
|
87
|
+
* @param {Array} response Response.
|
|
88
|
+
* @returns {File} File object or a null value.
|
|
89
|
+
*/
|
|
90
|
+
exports.copiedFile = response => {
|
|
91
|
+
if (typeof response[1] !== "undefined" && response[1].done) {
|
|
92
|
+
return response[0];
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
Storage
|
|
17
|
+
} = require("@google-cloud/storage");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets a Google Cloud Storage client with default settings.
|
|
21
|
+
*
|
|
22
|
+
* @returns {Storage} A Google Cloud Storage client.
|
|
23
|
+
*/
|
|
24
|
+
exports.getStorage = () => new Storage({
|
|
25
|
+
retryOptions: {
|
|
26
|
+
autoRetry: true,
|
|
27
|
+
retryDelayMultiplier: 4
|
|
28
|
+
}
|
|
29
|
+
});
|