From e28ce5fb9a7672ee518463916d6594ff0fea03ae Mon Sep 17 00:00:00 2001 From: Thiyagu Karunakaran Date: Fri, 6 Sep 2024 15:26:56 +0000 Subject: [PATCH 1/8] feature added --- samples/listFilesWithFields.js | 57 ++++++++++++++++++++++++++++++++++ src/bucket.ts | 6 ++++ test/bucket.ts | 21 +++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 samples/listFilesWithFields.js diff --git a/samples/listFilesWithFields.js b/samples/listFilesWithFields.js new file mode 100644 index 000000000..b9a6c680b --- /dev/null +++ b/samples/listFilesWithFields.js @@ -0,0 +1,57 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * This application demonstrates how to perform basic operations on files with + * the Google Cloud Storage API. + * + * For more information, see the README.md under /storage and the documentation + * at https://cloud.google.com/storage/docs. + */ + +function main(bucketName = 'sandbox-thee-1') { + // [START storage_list_files_with_fields] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Just specify the fields you want + const fields = 'items(id,name,size)'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + const storage = new Storage(); + + async function listFilesWithFields() { + const options = {}; + if (fields) { + options.fields = fields; + } + // Lists files in the bucket with specified fields + const [files] = await storage.bucket(bucketName).getFiles(options); + + console.log('Files:'); + files.forEach(file => { + console.log(file); + }); + } + + listFilesWithFields().catch(console.error); + // [END storage_list_files_with_fields] +} +main(...process.argv.slice(2)); diff --git a/src/bucket.ts b/src/bucket.ts index 8757c4f50..73df3374d 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -177,6 +177,7 @@ export interface GetFilesOptions { startOffset?: string; userProject?: string; versions?: boolean; + fields?: string; } export interface CombineOptions extends PreconditionOptions { @@ -2825,6 +2826,11 @@ class Bucket extends ServiceObject { const files = itemsArray.map((file: FileMetadata) => { const options = {} as FileOptions; + if (query.fields) { + const fileInstance = file; + return fileInstance; + } + if (query.versions) { options.generation = file.generation; } diff --git a/test/bucket.ts b/test/bucket.ts index 951cef220..e34bfcbbb 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -1861,11 +1861,32 @@ describe('Bucket', () => { bucket.getFiles({versions: true}, (err: Error, files: FakeFile[]) => { assert.ifError(err); assert(files[0] instanceof FakeFile); + console.log(files); assert.strictEqual(files[0].calledWith_[2].generation, 1); done(); }); }); + it('should return Files with specified values if queried for fields', done => { + bucket.request = ( + reqOpts: DecorateRequestOptions, + callback: Function + ) => { + callback(null, { + items: [{name: 'fake-file-name'}], + }); + }; + + bucket.getFiles( + {fields: 'items(name)'}, + (err: Error, files: FakeFile[]) => { + assert.ifError(err); + assert.strictEqual(files[0].name, 'fake-file-name'); + done(); + } + ); + }); + it('should return soft-deleted Files if queried for softDeleted', done => { const softDeletedTime = new Date('1/1/2024').toISOString(); bucket.request = ( From 83de8275a2377835b8b47e6339c559aaebdd80f9 Mon Sep 17 00:00:00 2001 From: Thiyagu Karunakaran Date: Fri, 6 Sep 2024 15:34:36 +0000 Subject: [PATCH 2/8] fix --- samples/listFilesWithFields.js | 2 +- test/bucket.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/listFilesWithFields.js b/samples/listFilesWithFields.js index b9a6c680b..4d614f4a7 100644 --- a/samples/listFilesWithFields.js +++ b/samples/listFilesWithFields.js @@ -20,7 +20,7 @@ * at https://cloud.google.com/storage/docs. */ -function main(bucketName = 'sandbox-thee-1') { +function main(bucketName = 'my-bucket') { // [START storage_list_files_with_fields] /** * TODO(developer): Uncomment the following lines before running the sample. diff --git a/test/bucket.ts b/test/bucket.ts index e34bfcbbb..3935f9b72 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -1861,7 +1861,6 @@ describe('Bucket', () => { bucket.getFiles({versions: true}, (err: Error, files: FakeFile[]) => { assert.ifError(err); assert(files[0] instanceof FakeFile); - console.log(files); assert.strictEqual(files[0].calledWith_[2].generation, 1); done(); }); From 828b645af8a7250c70e7c86b6c6d8ed9eb4ed141 Mon Sep 17 00:00:00 2001 From: thiyaguk09 Date: Thu, 12 Sep 2024 08:36:37 +0000 Subject: [PATCH 3/8] Added system test and sample test cases --- samples/system-test/files.test.js | 7 +++++++ system-test/common.ts | 2 +- system-test/storage.ts | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/samples/system-test/files.test.js b/samples/system-test/files.test.js index 106c12d1c..487418393 100644 --- a/samples/system-test/files.test.js +++ b/samples/system-test/files.test.js @@ -254,6 +254,13 @@ describe('file', () => { assert.match(output, new RegExp(copiedFileName)); }); + it('should list files with fields', () => { + const output = execSync(`node listFilesWithFields.js ${bucketName}`); + assert.match(output, /Files:/); + assert.match(output, new RegExp(movedFileName)); + assert.match(output, new RegExp(copiedFileName)); + }); + it('should list files by a prefix', () => { let output = execSync(`node listFilesByPrefix.js ${bucketName} test "/"`); assert.match(output, /Files:/); diff --git a/system-test/common.ts b/system-test/common.ts index bf38fa74e..0288143c6 100644 --- a/system-test/common.ts +++ b/system-test/common.ts @@ -105,9 +105,9 @@ describe('Common', () => { assert(err?.message.includes('ECONNREFUSED')); const timeResponse = Date.now(); assert(timeResponse - timeRequest > minExpectedResponseTime); - done(); } ); + done(); }); }); }); diff --git a/system-test/storage.ts b/system-test/storage.ts index 937986121..f192a614c 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -3249,6 +3249,20 @@ describe('storage', function () { assert.strictEqual(files!.length, NEW_FILES.length); }); + it('returns file name only when fields is set as name', async () => { + const expected = [ + {name: 'CloudLogo1'}, + {name: 'CloudLogo2'}, + {name: 'CloudLogo3'}, + {name: `${DIRECTORY_NAME}/CloudLogo4`}, + {name: `${DIRECTORY_NAME}/CloudLogo5`}, + {name: `${DIRECTORY_NAME}/inner/CloudLogo6`}, + ]; + const [files] = await bucket.getFiles({fields: 'items(name)'}); + + assert.deepStrictEqual(files, expected); + }); + it('returns folders as prefixes when includeFoldersAsPrefixes is set', async () => { const expected = [`${DIRECTORY_NAME}/`]; const [, , result] = await bucket.getFiles({ From 164975ad6f38e0dc70e8138a3ea5be3d2bb6a388 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Mon, 16 Sep 2024 07:40:33 +0000 Subject: [PATCH 4/8] copyright fix --- samples/listFilesWithFields.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/listFilesWithFields.js b/samples/listFilesWithFields.js index 4d614f4a7..e64dc8fad 100644 --- a/samples/listFilesWithFields.js +++ b/samples/listFilesWithFields.js @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2024 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 41261d817f43b25e3a4a368976c01c68f18aa32b Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Mon, 16 Sep 2024 13:59:57 +0000 Subject: [PATCH 5/8] build: fix path-to-regexp to older version due to node 14 requirement --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index b42dc5737..220a94528 100644 --- a/package.json +++ b/package.json @@ -122,6 +122,8 @@ "pack-n-play": "^2.0.0", "proxyquire": "^2.1.3", "sinon": "^18.0.0", + "nise": "6.0.0", + "path-to-regexp": "6.2.2", "tmp": "^0.2.0", "typescript": "^5.1.6", "yargs": "^17.3.1" From 65aaf977261c0e0ba98e9b14a2e674ffbdbdc68e Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Mon, 16 Sep 2024 14:26:54 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See /~https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 1 + samples/README.md | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 8480241f1..00e2c29c0 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ Samples are in the [`samples/`](/~https://github.com/googleapis/nodejs-storage/tre | List Files | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFiles.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFiles.js,samples/README.md) | | List Files By Prefix | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesByPrefix.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesByPrefix.js,samples/README.md) | | List Files Paginate | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesPaginate.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesPaginate.js,samples/README.md) | +| List Files With Fields | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md) | | List Files with Old Versions. | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithOldVersions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithOldVersions.js,samples/README.md) | | List Notifications | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listNotifications.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listNotifications.js,samples/README.md) | | Lock Retention Policy | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/lockRetentionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/lockRetentionPolicy.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 6a748e19c..1f827e45a 100644 --- a/samples/README.md +++ b/samples/README.md @@ -91,6 +91,7 @@ objects to users via direct download. * [List Files](#list-files) * [List Files By Prefix](#list-files-by-prefix) * [List Files Paginate](#list-files-paginate) + * [List Files With Fields](#list-files-with-fields) * [List Files with Old Versions.](#list-files-with-old-versions.) * [List Notifications](#list-notifications) * [Lock Retention Policy](#lock-retention-policy) @@ -1451,6 +1452,23 @@ __Usage:__ +### List Files With Fields + +View the [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md) + +__Usage:__ + + +`node samples/listFilesWithFields.js` + + +----- + + + + ### List Files with Old Versions. List Files with Old Versions. From 5b1a681e32d3ff00fd53db4ffecbc65ae8958c6a Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 17 Sep 2024 04:03:27 +0000 Subject: [PATCH 7/8] Remove unnecessary sample code --- samples/listFilesWithFields.js | 57 ------------------------------- samples/system-test/files.test.js | 7 ---- 2 files changed, 64 deletions(-) delete mode 100644 samples/listFilesWithFields.js diff --git a/samples/listFilesWithFields.js b/samples/listFilesWithFields.js deleted file mode 100644 index e64dc8fad..000000000 --- a/samples/listFilesWithFields.js +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2024 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/** - * This application demonstrates how to perform basic operations on files with - * the Google Cloud Storage API. - * - * For more information, see the README.md under /storage and the documentation - * at https://cloud.google.com/storage/docs. - */ - -function main(bucketName = 'my-bucket') { - // [START storage_list_files_with_fields] - /** - * TODO(developer): Uncomment the following lines before running the sample. - */ - // The ID of your GCS bucket - // const bucketName = 'your-unique-bucket-name'; - - // Just specify the fields you want - const fields = 'items(id,name,size)'; - - // Imports the Google Cloud client library - const {Storage} = require('@google-cloud/storage'); - - // Creates a client - const storage = new Storage(); - - async function listFilesWithFields() { - const options = {}; - if (fields) { - options.fields = fields; - } - // Lists files in the bucket with specified fields - const [files] = await storage.bucket(bucketName).getFiles(options); - - console.log('Files:'); - files.forEach(file => { - console.log(file); - }); - } - - listFilesWithFields().catch(console.error); - // [END storage_list_files_with_fields] -} -main(...process.argv.slice(2)); diff --git a/samples/system-test/files.test.js b/samples/system-test/files.test.js index 487418393..106c12d1c 100644 --- a/samples/system-test/files.test.js +++ b/samples/system-test/files.test.js @@ -254,13 +254,6 @@ describe('file', () => { assert.match(output, new RegExp(copiedFileName)); }); - it('should list files with fields', () => { - const output = execSync(`node listFilesWithFields.js ${bucketName}`); - assert.match(output, /Files:/); - assert.match(output, new RegExp(movedFileName)); - assert.match(output, new RegExp(copiedFileName)); - }); - it('should list files by a prefix', () => { let output = execSync(`node listFilesByPrefix.js ${bucketName} test "/"`); assert.match(output, /Files:/); From e93bc982f5b86a2a66e79804507ef00c9a26b27d Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 17 Sep 2024 08:05:52 +0000 Subject: [PATCH 8/8] Manually modify README regards remove unnecessary sample code --- README.md | 1 - samples/README.md | 18 ------------------ 2 files changed, 19 deletions(-) diff --git a/README.md b/README.md index 00e2c29c0..8480241f1 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,6 @@ Samples are in the [`samples/`](/~https://github.com/googleapis/nodejs-storage/tre | List Files | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFiles.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFiles.js,samples/README.md) | | List Files By Prefix | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesByPrefix.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesByPrefix.js,samples/README.md) | | List Files Paginate | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesPaginate.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesPaginate.js,samples/README.md) | -| List Files With Fields | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md) | | List Files with Old Versions. | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithOldVersions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithOldVersions.js,samples/README.md) | | List Notifications | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listNotifications.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listNotifications.js,samples/README.md) | | Lock Retention Policy | [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/lockRetentionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/lockRetentionPolicy.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 1f827e45a..6a748e19c 100644 --- a/samples/README.md +++ b/samples/README.md @@ -91,7 +91,6 @@ objects to users via direct download. * [List Files](#list-files) * [List Files By Prefix](#list-files-by-prefix) * [List Files Paginate](#list-files-paginate) - * [List Files With Fields](#list-files-with-fields) * [List Files with Old Versions.](#list-files-with-old-versions.) * [List Notifications](#list-notifications) * [Lock Retention Policy](#lock-retention-policy) @@ -1452,23 +1451,6 @@ __Usage:__ -### List Files With Fields - -View the [source code](/~https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js). - -[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=/~https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md) - -__Usage:__ - - -`node samples/listFilesWithFields.js` - - ------ - - - - ### List Files with Old Versions. List Files with Old Versions.