@pittica/google-cloud-storage-helpers 1.4.0 → 1.6.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
@@ -19,7 +19,8 @@ const {
19
19
  getFiles,
20
20
  listFiles,
21
21
  listFolders,
22
- copiedFile
22
+ copiedFile,
23
+ readFile
23
24
  } = require("./storage/file");
24
25
  const {
25
26
  downloadBucket,
@@ -30,6 +31,9 @@ const {
30
31
  writeJson,
31
32
  groupJson
32
33
  } = require("./storage/json");
34
+ const {
35
+ txtContentToArray
36
+ } = require("./storage/txt");
33
37
  const {
34
38
  getStorage,
35
39
  getBucket
@@ -41,10 +45,12 @@ exports.getFiles = getFiles;
41
45
  exports.listFiles = listFiles;
42
46
  exports.listFolders = listFolders;
43
47
  exports.copiedFile = copiedFile;
48
+ exports.readFile = readFile;
44
49
  exports.writeJson = writeJson;
45
50
  exports.groupJson = groupJson;
46
51
  exports.downloadFile = downloadFile;
47
52
  exports.downloadBucket = downloadBucket;
48
53
  exports.downloadFolder = downloadFolder;
49
54
  exports.getStorage = getStorage;
50
- exports.getBucket = getBucket;
55
+ exports.getBucket = getBucket;
56
+ exports.txtContentToArray = txtContentToArray;
package/dist/scripts.js CHANGED
@@ -21,5 +21,6 @@ const {
21
21
  *
22
22
  * @param {string} name Bucket name.
23
23
  * @param {string} destination Destination path.
24
+ * @param {string} projectId Optional project ID.
24
25
  */
25
- exports.download = async (name, destination = __dirname) => downloadBucket(name, destination);
26
+ exports.download = async (name, destination = __dirname, projectId = null) => downloadBucket(name, destination, projectId);
@@ -14,7 +14,6 @@
14
14
 
15
15
  const path = require("path");
16
16
  const {
17
- Storage,
18
17
  TransferManager
19
18
  } = require("@google-cloud/storage");
20
19
  const log = require("@pittica/logger-helpers");
@@ -25,9 +24,10 @@ const fs = require("fs");
25
24
  *
26
25
  * @param {string} bucket Bucket name.
27
26
  * @param {string} destination Destination path.
27
+ * @param {string} projectId Optional project ID.
28
28
  */
29
- exports.downloadBucket = async (bucket, destination = __dirname) => {
30
- const storage = new Storage();
29
+ exports.downloadBucket = async (bucket, destination = __dirname, projectId = null) => {
30
+ const storage = getStorage(projectId);
31
31
  const source = storage.bucket(bucket);
32
32
  const [files] = await source.getFiles();
33
33
  files.forEach(({
@@ -59,10 +59,11 @@ exports.downloadFile = async (file, bucket, directory = __dirname) => {
59
59
  * @param {string} folder Folder name.
60
60
  * @param {Bucket} bucket Google Cloud Storage bucket object.
61
61
  * @param {string} directory Destination directory.
62
+ * @param {string} projectId Optional project ID.
62
63
  * @returns {Promise} Response.
63
64
  */
64
- exports.downloadFolder = (folder, bucket, directory = __dirname) => {
65
- const storage = new Storage();
65
+ exports.downloadFolder = (folder, bucket, directory = __dirname, projectId = null) => {
66
+ const storage = getStorage(projectId);
66
67
  const manager = new TransferManager(storage.bucket(bucket));
67
68
  const destination = path.join(directory, folder);
68
69
  if (!fs.existsSync(destination)) {
@@ -12,12 +12,10 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
- const {
16
- Storage
17
- } = require("@google-cloud/storage");
18
15
  const log = require("@pittica/logger-helpers");
19
16
  const {
20
- getStorage
17
+ getStorage,
18
+ getBucket
21
19
  } = require("./storage");
22
20
 
23
21
  /**
@@ -25,9 +23,10 @@ const {
25
23
  *
26
24
  * @param {string} source Source bucket.
27
25
  * @param {string} destination Destination bucket.
26
+ * @param {string} projectId Optional project ID.
28
27
  */
29
- exports.moveFiles = async (source, destination) => {
30
- const storage = getStorage();
28
+ exports.moveFiles = async (source, destination, projectId = null) => {
29
+ const storage = getStorage(projectId);
31
30
  const bucket = storage.bucket(destination);
32
31
  const [files] = await storage.bucket(source).getFiles();
33
32
  files.forEach(results => Array.isArray(results) ? results.forEach(file => exports.moveFile(file, bucket)) : exports.moveFile(results, bucket));
@@ -87,10 +86,11 @@ exports.getFiles = (storage, source, prefix) => storage.bucket(source).getFiles(
87
86
  *
88
87
  * @param {string} bucket Bucket name.
89
88
  * @param {string} prefix Files prefix.
89
+ * @param {string} projectId Optional project ID.
90
90
  * @returns {Array} Files from the given bucket.
91
91
  * @since 1.3.0
92
92
  */
93
- exports.listFiles = (source, prefix) => getStorage().bucket(source).getFiles({
93
+ exports.listFiles = (source, prefix, projectId = null) => getStorage(projectId).bucket(source).getFiles({
94
94
  prefix,
95
95
  autoPaginate: false,
96
96
  fields: "items(name)"
@@ -106,10 +106,11 @@ exports.listFiles = (source, prefix) => getStorage().bucket(source).getFiles({
106
106
  *
107
107
  * @param {string} bucket Bucket name.
108
108
  * @param {string} prefix Files prefix.
109
+ * @param {string} projectId Optional project ID.
109
110
  * @returns {Array} Folders from the given bucket.
110
111
  * @since 1.3.0
111
112
  */
112
- exports.listFolders = async (source, prefix = "") => await getStorage().bucket(source).getFiles({
113
+ exports.listFolders = async (source, prefix = "", projectId = null) => await getStorage(projectId).bucket(source).getFiles({
113
114
  prefix,
114
115
  autoPaginate: false,
115
116
  delimiter: "/",
@@ -132,4 +133,18 @@ exports.copiedFile = response => {
132
133
  return response[0];
133
134
  }
134
135
  return null;
136
+ };
137
+
138
+ /**
139
+ * Downloads a file from Google Cloud Storage.
140
+ *
141
+ * @param {string} file File name and path.
142
+ * @param {string} bucket Bucket name.
143
+ * @param {string} projectId Optional project ID.
144
+ * @returns {Buffer} File content.
145
+ * @since 1.5.0
146
+ */
147
+ exports.readFile = async (file, bucket, projectId = null) => {
148
+ const [contents] = await getBucket(bucket, projectId).file(file).download();
149
+ return Buffer.from(contents);
135
150
  };
@@ -13,35 +13,46 @@
13
13
  // limitations under the License.
14
14
 
15
15
  const {
16
- Storage
17
- } = require("@google-cloud/storage");
16
+ finished
17
+ } = require("stream/promises");
18
18
  const log = require("@pittica/logger-helpers");
19
+ const {
20
+ getStorage
21
+ } = require("./storage");
19
22
 
20
23
  /**
21
24
  * Writes JSON data.
22
25
  *
23
26
  * @param {string} file File name and path.
24
27
  * @param {object} body Document body.
25
- *
26
- * @returns {boolean} Write action response.
28
+ * @param {string} bucket Bucket name.
29
+ * @param {string} projectId Optional project ID.
30
+ * @returns {boolean} A value indicating whether the operation was successful.
27
31
  */
28
- exports.writeJson = async (file, body, bucket) => {
29
- const storage = new Storage();
32
+ exports.writeJson = async (file, body, bucket, projectId = null) => {
33
+ const buffer = Buffer.from(JSON.stringify(body));
34
+ const storage = getStorage(projectId);
30
35
  const stream = storage.bucket(bucket).file(file).createWriteStream({
31
36
  metadata: {
32
37
  contentType: "application/json"
33
38
  }
34
39
  });
35
- stream.on("error", error => log.error(error));
36
- stream.on("finish", () => log.success(`"${file}" has been written.`));
37
- const buffer = Buffer.from(JSON.stringify(body));
38
- return stream.end(buffer);
40
+ try {
41
+ stream.end(buffer);
42
+ await finished(stream);
43
+ log.success(`"${file}" has been written.`);
44
+ return true;
45
+ } catch (error) {
46
+ log.error(error);
47
+ return false;
48
+ }
39
49
  };
40
50
 
41
51
  /**
42
52
  * Group files by table.
43
53
  *
44
54
  * @param {Array} files An array of files.
55
+ * @returns {Object} An object with grouped files.
45
56
  */
46
57
  exports.groupJson = files => {
47
58
  const result = {};
@@ -19,9 +19,11 @@ const {
19
19
  /**
20
20
  * Gets a Google Cloud Storage client with default settings.
21
21
  *
22
+ * @param {string} projectId Optional project ID.
22
23
  * @returns {Storage} A Google Cloud Storage client.
23
24
  */
24
- exports.getStorage = () => new Storage({
25
+ exports.getStorage = (projectId = null) => new Storage({
26
+ projectId,
25
27
  retryOptions: {
26
28
  autoRetry: true,
27
29
  retryDelayMultiplier: 4
@@ -32,7 +34,8 @@ exports.getStorage = () => new Storage({
32
34
  * Gets a Google Cloud Storage bucket.
33
35
  *
34
36
  * @param {string} bucket Bucket name.
37
+ * @param {string} projectId Optional project ID.
35
38
  * @returns {Storage} A Google Cloud Storage client.
36
39
  * @since 1.4.0
37
40
  */
38
- exports.getBucket = bucket => exports.getStorage().bucket(bucket);
41
+ exports.getBucket = (bucket, projectId = null) => exports.getStorage(projectId).bucket(bucket);
@@ -0,0 +1,30 @@
1
+ // Copyright 2024-2026 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
+ readFile
17
+ } = require("./file");
18
+
19
+ /**
20
+ * Converts text content to an array.
21
+ *
22
+ * @param {string} file File name and path.
23
+ * @param {string} bucket Bucket name.
24
+ * @param {string} projectId Optional project ID.
25
+ * @returns {Array} File content.
26
+ */
27
+ exports.txtContentToArray = async (file, bucket, projectId = null) => {
28
+ const buffer = await readFile(file, bucket, projectId);
29
+ return buffer.toString().split("\n").filter(line => line.trim() !== "");
30
+ };
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.4.0",
4
+ "version": "1.6.0",
5
5
  "description": "Helpers for Google Cloud Storage.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
@@ -26,13 +26,13 @@
26
26
  },
27
27
  "homepage": "https://github.com/pittica/google-cloud-storage-helpers#README.md",
28
28
  "dependencies": {
29
- "@google-cloud/storage": "^7.21.0",
30
- "@pittica/logger-helpers": "^1.0.5"
29
+ "@google-cloud/storage": "^8.4.0",
30
+ "@pittica/logger-helpers": "^1.2.2"
31
31
  },
32
32
  "devDependencies": {
33
- "@babel/cli": "^7.29.7",
33
+ "@babel/cli": "^8.0.6",
34
34
  "mkdirp": "^3.0.1",
35
- "prettier": "^3.9.0",
35
+ "prettier": "^3.9.10",
36
36
  "rimraf": "^6.1.3",
37
37
  "run-func": "^3.0.0"
38
38
  }