@pittica/google-cloud-storage-helpers 1.1.0 → 1.2.0

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 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;
@@ -59,4 +59,29 @@ exports.copyFile = (source, destination) => source.copy(source.storage.bucket(de
59
59
  exports.deleteFile = file => file.delete().then(response => {
60
60
  const [res] = response;
61
61
  return res.statusCode >= 200 && res.statusCode < 300;
62
- }).catch(() => false);
62
+ }).catch(() => false);
63
+
64
+ /**
65
+ * Gets the files from the given bucket.
66
+ *
67
+ * @param {Storage} storage Google Cloud Storage object.
68
+ * @param {string} bucket Bucket name.
69
+ * @param {string} prefix Files prefix.
70
+ * @returns {Array} Files from the given bucket.
71
+ */
72
+ exports.getFiles = (storage, source, prefix) => storage.bucket(source).getFiles({
73
+ prefix
74
+ }).then(files => files).catch(() => []);
75
+
76
+ /**
77
+ * Process the response from copy API and extracts the response file object.
78
+ *
79
+ * @param {Array} response Response.
80
+ * @returns {File} File object or a null value.
81
+ */
82
+ exports.copiedFile = response => {
83
+ if (typeof response[1] !== "undefined" && response[1].done) {
84
+ return response[0];
85
+ }
86
+ return null;
87
+ };
@@ -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
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pittica/google-cloud-storage-helpers",
3
3
  "private": false,
4
- "version": "1.1.0",
4
+ "version": "1.2.0",
5
5
  "description": "Helpers for Google Cloud Storage.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {