From a7df900ca9ba6ed731b844687e583abc37878f9a Mon Sep 17 00:00:00 2001 From: Shubhangi singh Date: Sun, 8 Sep 2024 17:31:04 +0000 Subject: [PATCH 1/8] adding functionality to fetch details from gs url --- .../lib/google/cloud/storage/file.rb | 43 +++++++++++++++++++ .../samples/acceptance/files_test.rb | 8 ++++ .../samples/storage_fetch_from_gs_url.rb | 35 +++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 google-cloud-storage/samples/storage_fetch_from_gs_url.rb diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index 3e57543dcf9c..dc5df0fe3fcd 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2081,6 +2081,49 @@ def self.gapi_from_attrs gapi, attributes Google::Apis::StorageV1::Object.new(**attr_params) end + ## + # from_gs_url is a method to fetch bucket details and file details from a gs url + # + # @return [Hash(String => String)] + # + # @example Fetch bucketName and fileName from gs url: + # require "google/cloud/storage" + # gs_url= "gs://my-todo-app/avatars/heidi.jpeg" + # fileObject=Google::Cloud::Storage::File + # fileObject.from_gs_url(gs_url) + # => + # {"bucket_name"=>"test_nucket_2", "file_name"=>"floer.jpeg"} + # + # @example Fetch bucketName , fileName and other query params from gs url: + # require "google/cloud/storage" + # gs_url= "gs://test_nucket_2/test_sub_folder/floer.jpeg?params1=test1¶ms2=test2" + # fileObject=Google::Cloud::Storage::File + # fileObject.from_gs_url(gs_url) + # => + # {"bucket_name"=>"test_nucket_2", "file_name"=>"floer.jpeg", "params1"=>"test1", "params2"=>"test2"} + + + def self.from_gs_url url + prefix = "gs://".freeze + # seprating params from input url + parsed_url = URI.parse(url) + query_params = URI.decode_www_form(parsed_url.query).to_h if parsed_url.query + parsed_url.query= nil + url= parsed_url.to_s + # parsing the url + object_uri = url.split(prefix)[1] + bucket_name = object_uri.split("/")[0] + file_name = object_uri.split("/").last + url_items = { + 'bucket_name'=> bucket_name, + 'file_name'=> file_name + } + # adding url params to output hash + url_items.merge!(query_params) if query_params + return url_items + end + + protected ## diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index 3742fc87dcd8..8ffb3f0b629e 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -26,6 +26,7 @@ require_relative "../storage_download_file_into_memory" require_relative "../storage_download_file_requester_pays" require_relative "../storage_download_public_file" +require_relative "../storage_fetch_from_gs_url" require_relative "../storage_generate_encryption_key" require_relative "../storage_generate_signed_post_policy_v4" require_relative "../storage_generate_signed_url_v4" @@ -59,6 +60,7 @@ let(:downloaded_file) { "test_download_#{SecureRandom.hex}" } let(:file_1_name) { "path/file_1_name_#{SecureRandom.hex}.txt" } let(:file_2_name) { "path/file_2_name_#{SecureRandom.hex}.txt" } + let(:gs_url) {"gs://test_nucket_2/test_sub_folder/floer.jpeg"} let(:bucket) { @bucket } let(:secondary_bucket) { @secondary_bucket } let(:file_content) { "some content" } @@ -273,6 +275,12 @@ def mock_cipher.random_key assert_nil bucket.file remote_file_name end + it "fetch_from_gs_url" do + fetch_from_gs_url gs_url + + assert_output "Output json {'bucket_name'=>'test_nucket_2', 'file_name'=>'floer.jpeg'}" + end + it "get_metadata" do bucket.create_file local_file, remote_file_name diff --git a/google-cloud-storage/samples/storage_fetch_from_gs_url.rb b/google-cloud-storage/samples/storage_fetch_from_gs_url.rb new file mode 100644 index 000000000000..ad6232e405ba --- /dev/null +++ b/google-cloud-storage/samples/storage_fetch_from_gs_url.rb @@ -0,0 +1,35 @@ +# Copyright 2022 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. + +# Sample for storage_fetch_from_gs_url +def fetch_from_gs_url url + # [START storage_fetch_from_gs_url] + # The ID of your GCS bucket + # bucket_name = "your-unique-bucket-name" + # file_name = "Name of a file in the Storage bucket" + # email = "Google Cloud Storage ACL Entity email" + + require "google/cloud/storage" + + fileObject = Google::Cloud::Storage::File + output = fileObject.from_gs_url url + + puts "Output json #{output}" + # [END storage_fetch_from_gs_url] +end + +if $PROGRAM_NAME == __FILE__ + fetch_from_gs_url url: ARGV.shift +end + From c2f0949d5faf0f44c0dfdaf601d2fd4e4078d4ca Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 11 Sep 2024 05:45:49 +0000 Subject: [PATCH 2/8] adding testcases for fetch details from gs url --- .../lib/google/cloud/storage/file.rb | 36 +++++++++++-------- .../samples/acceptance/files_test.rb | 2 +- .../samples/storage_fetch_from_gs_url.rb | 25 ++++++------- .../test/google/cloud/storage/file_test.rb | 13 +++++++ 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index dc5df0fe3fcd..0a50d7581aa0 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2082,7 +2082,7 @@ def self.gapi_from_attrs gapi, attributes end ## - # from_gs_url is a method to fetch bucket details and file details from a gs url + # from_gs_url is a method to fetch bucket details and file details from a gs url # # @return [Hash(String => String)] # @@ -2091,36 +2091,42 @@ def self.gapi_from_attrs gapi, attributes # gs_url= "gs://my-todo-app/avatars/heidi.jpeg" # fileObject=Google::Cloud::Storage::File # fileObject.from_gs_url(gs_url) - # => - # {"bucket_name"=>"test_nucket_2", "file_name"=>"floer.jpeg"} + # => + # {"bucket_name"=>"my-todo-app", "file_name"=>"heidi.jpeg"} # # @example Fetch bucketName , fileName and other query params from gs url: # require "google/cloud/storage" - # gs_url= "gs://test_nucket_2/test_sub_folder/floer.jpeg?params1=test1¶ms2=test2" + # gs_url= "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1¶ms2=test2" # fileObject=Google::Cloud::Storage::File # fileObject.from_gs_url(gs_url) - # => - # {"bucket_name"=>"test_nucket_2", "file_name"=>"floer.jpeg", "params1"=>"test1", "params2"=>"test2"} - + # =>{ + # "bucket_name"=>"my-todo-app", + # "file_name"=>"heidi.jpeg", + # "options" => { + # "params1"=>"test1", + # "params2"=>"test2" + # } + # } def self.from_gs_url url prefix = "gs://".freeze # seprating params from input url - parsed_url = URI.parse(url) - query_params = URI.decode_www_form(parsed_url.query).to_h if parsed_url.query - parsed_url.query= nil - url= parsed_url.to_s + # encoded_url = url.strip.gsub('@','%40') + parsed_url = URI.parse url + query_params = URI.decode_www_form(parsed_url.query).to_h if parsed_url.query + parsed_url.query = nil + url = parsed_url.to_s # parsing the url object_uri = url.split(prefix)[1] bucket_name = object_uri.split("/")[0] file_name = object_uri.split("/").last url_items = { - 'bucket_name'=> bucket_name, - 'file_name'=> file_name + "bucket_name" => bucket_name, + "file_name" => file_name } # adding url params to output hash - url_items.merge!(query_params) if query_params - return url_items + url_items.merge! "options" => query_params if query_params + url_items end diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index 8ffb3f0b629e..b1258c07b072 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -60,7 +60,7 @@ let(:downloaded_file) { "test_download_#{SecureRandom.hex}" } let(:file_1_name) { "path/file_1_name_#{SecureRandom.hex}.txt" } let(:file_2_name) { "path/file_2_name_#{SecureRandom.hex}.txt" } - let(:gs_url) {"gs://test_nucket_2/test_sub_folder/floer.jpeg"} + let(:gs_url) { "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1¶ms2=test2" } let(:bucket) { @bucket } let(:secondary_bucket) { @secondary_bucket } let(:file_content) { "some content" } diff --git a/google-cloud-storage/samples/storage_fetch_from_gs_url.rb b/google-cloud-storage/samples/storage_fetch_from_gs_url.rb index ad6232e405ba..dafee9077acf 100644 --- a/google-cloud-storage/samples/storage_fetch_from_gs_url.rb +++ b/google-cloud-storage/samples/storage_fetch_from_gs_url.rb @@ -14,22 +14,19 @@ # Sample for storage_fetch_from_gs_url def fetch_from_gs_url url - # [START storage_fetch_from_gs_url] - # The ID of your GCS bucket - # bucket_name = "your-unique-bucket-name" - # file_name = "Name of a file in the Storage bucket" - # email = "Google Cloud Storage ACL Entity email" + # [START storage_fetch_from_gs_url] + # bucket_name = "your-unique-bucket-name" + # file_name = "Name of a file in the Storage bucket" + # gs_url= "gs://#{bucket_name}//#{file_name}" + require "google/cloud/storage" - require "google/cloud/storage" + file_object = Google::Cloud::Storage::File + output = file_object.from_gs_url url - fileObject = Google::Cloud::Storage::File - output = fileObject.from_gs_url url - - puts "Output json #{output}" - # [END storage_fetch_from_gs_url] + puts "Output json #{output}" + # [END storage_fetch_from_gs_url] end - + if $PROGRAM_NAME == __FILE__ - fetch_from_gs_url url: ARGV.shift + fetch_from_gs_url url: ARGV.shift end - diff --git a/google-cloud-storage/test/google/cloud/storage/file_test.rb b/google-cloud-storage/test/google/cloud/storage/file_test.rb index 06f541229ea0..b7c5e854b232 100644 --- a/google-cloud-storage/test/google/cloud/storage/file_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/file_test.rb @@ -1571,6 +1571,19 @@ def file_user_project.sleep *args end end + describe "fetch details from gs_url" do + let(:bucket_name) { "object-lock-bucket" } + let(:file_name) {"file.ext"} + let(:file_object) {Google::Cloud::Storage::File} + let(:gs_url) {"gs://#{bucket_name}/#{file_name}"} + + it "it returns file_name and bucket_name from given gs url " do + url_items= file_object.from_gs_url(gs_url) + assert_equal bucket_name, url_items["bucket_name"] + assert_equal file_name, url_items["file_name"] + end + end + def gzip_data data gz = StringIO.new("") z = Zlib::GzipWriter.new(gz) From 7cb87825ba2df8d11a0c5aba8532a858132e7bc9 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 11 Sep 2024 11:16:41 +0000 Subject: [PATCH 3/8] removing unwanted samples created --- .../samples/acceptance/files_test.rb | 7 ---- .../samples/storage_fetch_from_gs_url.rb | 32 ------------------- 2 files changed, 39 deletions(-) delete mode 100644 google-cloud-storage/samples/storage_fetch_from_gs_url.rb diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index b1258c07b072..979d2133fdc2 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -60,7 +60,6 @@ let(:downloaded_file) { "test_download_#{SecureRandom.hex}" } let(:file_1_name) { "path/file_1_name_#{SecureRandom.hex}.txt" } let(:file_2_name) { "path/file_2_name_#{SecureRandom.hex}.txt" } - let(:gs_url) { "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1¶ms2=test2" } let(:bucket) { @bucket } let(:secondary_bucket) { @secondary_bucket } let(:file_content) { "some content" } @@ -275,12 +274,6 @@ def mock_cipher.random_key assert_nil bucket.file remote_file_name end - it "fetch_from_gs_url" do - fetch_from_gs_url gs_url - - assert_output "Output json {'bucket_name'=>'test_nucket_2', 'file_name'=>'floer.jpeg'}" - end - it "get_metadata" do bucket.create_file local_file, remote_file_name diff --git a/google-cloud-storage/samples/storage_fetch_from_gs_url.rb b/google-cloud-storage/samples/storage_fetch_from_gs_url.rb deleted file mode 100644 index dafee9077acf..000000000000 --- a/google-cloud-storage/samples/storage_fetch_from_gs_url.rb +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2022 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. - -# Sample for storage_fetch_from_gs_url -def fetch_from_gs_url url - # [START storage_fetch_from_gs_url] - # bucket_name = "your-unique-bucket-name" - # file_name = "Name of a file in the Storage bucket" - # gs_url= "gs://#{bucket_name}//#{file_name}" - require "google/cloud/storage" - - file_object = Google::Cloud::Storage::File - output = file_object.from_gs_url url - - puts "Output json #{output}" - # [END storage_fetch_from_gs_url] -end - -if $PROGRAM_NAME == __FILE__ - fetch_from_gs_url url: ARGV.shift -end From 345110ac10a9909bc319730b4563f7f90a220638 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Wed, 11 Sep 2024 11:18:13 +0000 Subject: [PATCH 4/8] removing unwanted samples created --- google-cloud-storage/samples/acceptance/files_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index 979d2133fdc2..3742fc87dcd8 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -26,7 +26,6 @@ require_relative "../storage_download_file_into_memory" require_relative "../storage_download_file_requester_pays" require_relative "../storage_download_public_file" -require_relative "../storage_fetch_from_gs_url" require_relative "../storage_generate_encryption_key" require_relative "../storage_generate_signed_post_policy_v4" require_relative "../storage_generate_signed_url_v4" From 6d77ef3d1e5ae54cc60324f65b704a175814b40a Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Mon, 16 Sep 2024 09:27:45 +0000 Subject: [PATCH 5/8] code refactoring --- .../lib/google/cloud/storage/file.rb | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index 0a50d7581aa0..3882bf5306d4 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2086,19 +2086,19 @@ def self.gapi_from_attrs gapi, attributes # # @return [Hash(String => String)] # - # @example Fetch bucketName and fileName from gs url: + # @example Fetch bucket_name and file_name from gs url: # require "google/cloud/storage" # gs_url= "gs://my-todo-app/avatars/heidi.jpeg" - # fileObject=Google::Cloud::Storage::File - # fileObject.from_gs_url(gs_url) + # file=Google::Cloud::Storage::File + # file.from_gs_url(gs_url) # => # {"bucket_name"=>"my-todo-app", "file_name"=>"heidi.jpeg"} # - # @example Fetch bucketName , fileName and other query params from gs url: + # @example Fetch bucket_name , file_name and other query params from gs url: # require "google/cloud/storage" # gs_url= "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1¶ms2=test2" - # fileObject=Google::Cloud::Storage::File - # fileObject.from_gs_url(gs_url) + # file=Google::Cloud::Storage::File + # file.from_gs_url(gs_url) # =>{ # "bucket_name"=>"my-todo-app", # "file_name"=>"heidi.jpeg", @@ -2108,28 +2108,24 @@ def self.gapi_from_attrs gapi, attributes # } # } - def self.from_gs_url url + def self.from_gs_url gs_url prefix = "gs://".freeze + raise ArgumentError, "Invalid GCS URL" unless gs_url.start_with? prefix # seprating params from input url - # encoded_url = url.strip.gsub('@','%40') - parsed_url = URI.parse url - query_params = URI.decode_www_form(parsed_url.query).to_h if parsed_url.query - parsed_url.query = nil - url = parsed_url.to_s + path, query = gs_url.sub(prefix, "").split("?", 2) # parsing the url - object_uri = url.split(prefix)[1] - bucket_name = object_uri.split("/")[0] - file_name = object_uri.split("/").last + bucket_name, file_path = path.split "/", 2 + query_params = URI.decode_www_form(query).to_h if query + file_name = file_path.split("/").last url_items = { "bucket_name" => bucket_name, "file_name" => file_name } # adding url params to output hash - url_items.merge! "options" => query_params if query_params + url_items.merge! "options" => query_params if query url_items end - protected ## From 5241b64a54b034bbc8578dec31ef20021344c7d6 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 19 Sep 2024 12:20:24 +0000 Subject: [PATCH 6/8] returning file_path in case file is within a subfolder --- .../lib/google/cloud/storage/file.rb | 7 ++-- .../test/google/cloud/storage/file_test.rb | 37 ++++++++++++++++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index 3882bf5306d4..f2883a8e742f 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2092,7 +2092,7 @@ def self.gapi_from_attrs gapi, attributes # file=Google::Cloud::Storage::File # file.from_gs_url(gs_url) # => - # {"bucket_name"=>"my-todo-app", "file_name"=>"heidi.jpeg"} + # {"bucket_name"=>"my-todo-app", "file_name"=>"avatars/heidi.jpeg"} # # @example Fetch bucket_name , file_name and other query params from gs url: # require "google/cloud/storage" @@ -2101,7 +2101,7 @@ def self.gapi_from_attrs gapi, attributes # file.from_gs_url(gs_url) # =>{ # "bucket_name"=>"my-todo-app", - # "file_name"=>"heidi.jpeg", + # "file_name"=>"test_sub_folder/heidi.jpeg", # "options" => { # "params1"=>"test1", # "params2"=>"test2" @@ -2116,10 +2116,9 @@ def self.from_gs_url gs_url # parsing the url bucket_name, file_path = path.split "/", 2 query_params = URI.decode_www_form(query).to_h if query - file_name = file_path.split("/").last url_items = { "bucket_name" => bucket_name, - "file_name" => file_name + "file_name" => file_path } # adding url params to output hash url_items.merge! "options" => query_params if query diff --git a/google-cloud-storage/test/google/cloud/storage/file_test.rb b/google-cloud-storage/test/google/cloud/storage/file_test.rb index b7c5e854b232..2c8260a982f7 100644 --- a/google-cloud-storage/test/google/cloud/storage/file_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/file_test.rb @@ -1573,14 +1573,41 @@ def file_user_project.sleep *args describe "fetch details from gs_url" do let(:bucket_name) { "object-lock-bucket" } - let(:file_name) {"file.ext"} - let(:file_object) {Google::Cloud::Storage::File} - let(:gs_url) {"gs://#{bucket_name}/#{file_name}"} + let(:file_name) {"file.jpeg"} + let(:file) {Google::Cloud::Storage::File} + let(:param) {"param1"} + let(:param_val) {"test"} + + it "it returns file_name and bucket_name from given gs url" do + gs_url = "gs://#{bucket_name}/#{file_name}" + url_items = file.from_gs_url gs_url + assert_equal bucket_name, url_items["bucket_name"] + assert_equal file_name, url_items["file_name"] + end - it "it returns file_name and bucket_name from given gs url " do - url_items= file_object.from_gs_url(gs_url) + it "it returns file_name, bucket_name and url params in options hash from given gs url with parameters" do + gs_url= "gs://#{bucket_name}/#{file_name}?#{param}=#{param_val}" + url_items = file.from_gs_url gs_url assert_equal bucket_name, url_items["bucket_name"] assert_equal file_name, url_items["file_name"] + expected_params_hash_in_output = {'param1' =>'test'} + assert_equal expected_params_hash_in_output, url_items["options"] + end + + it "it returns file_path with subfolder name and file name and bucket_name from given gs url" do + bucket_subfolder_name = "avatars" + gs_url = "gs://#{bucket_name}/#{bucket_subfolder_name}/#{file_name}" + url_items = file.from_gs_url gs_url + expected_file_name = "#{bucket_subfolder_name}/#{file_name}" + assert_equal bucket_name, url_items["bucket_name"] + assert_equal expected_file_name, url_items["file_name"] + end + + it "raises error if url provided is not a valid gs url" do + invalid_gs_url = "http://my_bucket/my_file.txt" + assert_raises ArgumentError do + file.from_gs_url invalid_gs_url + end end end From 0d18569386f087308a2bf9288b14355b0f7b442a Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Thu, 19 Sep 2024 14:33:12 +0000 Subject: [PATCH 7/8] fixing variable name --- .../lib/google/cloud/storage/file.rb | 6 +++--- .../test/google/cloud/storage/file_test.rb | 19 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index f2883a8e742f..dde29dd5244e 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2092,7 +2092,7 @@ def self.gapi_from_attrs gapi, attributes # file=Google::Cloud::Storage::File # file.from_gs_url(gs_url) # => - # {"bucket_name"=>"my-todo-app", "file_name"=>"avatars/heidi.jpeg"} + # {"bucket_name"=>"my-todo-app", "file_path"=>"avatars/heidi.jpeg"} # # @example Fetch bucket_name , file_name and other query params from gs url: # require "google/cloud/storage" @@ -2101,7 +2101,7 @@ def self.gapi_from_attrs gapi, attributes # file.from_gs_url(gs_url) # =>{ # "bucket_name"=>"my-todo-app", - # "file_name"=>"test_sub_folder/heidi.jpeg", + # "file_path"=>"test_sub_folder/heidi.jpeg", # "options" => { # "params1"=>"test1", # "params2"=>"test2" @@ -2118,7 +2118,7 @@ def self.from_gs_url gs_url query_params = URI.decode_www_form(query).to_h if query url_items = { "bucket_name" => bucket_name, - "file_name" => file_path + "file_path" => file_path } # adding url params to output hash url_items.merge! "options" => query_params if query diff --git a/google-cloud-storage/test/google/cloud/storage/file_test.rb b/google-cloud-storage/test/google/cloud/storage/file_test.rb index 2c8260a982f7..4b52e9954b82 100644 --- a/google-cloud-storage/test/google/cloud/storage/file_test.rb +++ b/google-cloud-storage/test/google/cloud/storage/file_test.rb @@ -1572,35 +1572,34 @@ def file_user_project.sleep *args end describe "fetch details from gs_url" do - let(:bucket_name) { "object-lock-bucket" } - let(:file_name) {"file.jpeg"} + let(:bucket_name) { "my-random-bucket" } + let(:file_path) {"file.jpeg"} let(:file) {Google::Cloud::Storage::File} let(:param) {"param1"} let(:param_val) {"test"} + let(:gs_url) {"gs://#{bucket_name}/#{file_path}"} it "it returns file_name and bucket_name from given gs url" do - gs_url = "gs://#{bucket_name}/#{file_name}" url_items = file.from_gs_url gs_url assert_equal bucket_name, url_items["bucket_name"] - assert_equal file_name, url_items["file_name"] + assert_equal file_path, url_items["file_path"] end it "it returns file_name, bucket_name and url params in options hash from given gs url with parameters" do - gs_url= "gs://#{bucket_name}/#{file_name}?#{param}=#{param_val}" + gs_url= "gs://#{bucket_name}/#{file_path}?#{param}=#{param_val}" url_items = file.from_gs_url gs_url assert_equal bucket_name, url_items["bucket_name"] - assert_equal file_name, url_items["file_name"] + assert_equal file_path, url_items["file_path"] expected_params_hash_in_output = {'param1' =>'test'} assert_equal expected_params_hash_in_output, url_items["options"] end it "it returns file_path with subfolder name and file name and bucket_name from given gs url" do - bucket_subfolder_name = "avatars" - gs_url = "gs://#{bucket_name}/#{bucket_subfolder_name}/#{file_name}" + file_path = "avatars/#{file_path}" + gs_url = "gs://#{bucket_name}/#{file_path}" url_items = file.from_gs_url gs_url - expected_file_name = "#{bucket_subfolder_name}/#{file_name}" assert_equal bucket_name, url_items["bucket_name"] - assert_equal expected_file_name, url_items["file_name"] + assert_equal file_path, url_items["file_path"] end it "raises error if url provided is not a valid gs url" do From 5b8b18cbae733767a066aa475a163d4157ced0d4 Mon Sep 17 00:00:00 2001 From: Shubhangi Singh Date: Fri, 20 Sep 2024 07:41:49 +0000 Subject: [PATCH 8/8] renaming var name in examples --- google-cloud-storage/lib/google/cloud/storage/file.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-storage/lib/google/cloud/storage/file.rb b/google-cloud-storage/lib/google/cloud/storage/file.rb index dde29dd5244e..11bdc3d30455 100644 --- a/google-cloud-storage/lib/google/cloud/storage/file.rb +++ b/google-cloud-storage/lib/google/cloud/storage/file.rb @@ -2086,7 +2086,7 @@ def self.gapi_from_attrs gapi, attributes # # @return [Hash(String => String)] # - # @example Fetch bucket_name and file_name from gs url: + # @example Fetch bucket_name and file_Path from gs url: # require "google/cloud/storage" # gs_url= "gs://my-todo-app/avatars/heidi.jpeg" # file=Google::Cloud::Storage::File @@ -2094,7 +2094,7 @@ def self.gapi_from_attrs gapi, attributes # => # {"bucket_name"=>"my-todo-app", "file_path"=>"avatars/heidi.jpeg"} # - # @example Fetch bucket_name , file_name and other query params from gs url: + # @example Fetch bucket_name , file_Path and other query params from gs url: # require "google/cloud/storage" # gs_url= "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1¶ms2=test2" # file=Google::Cloud::Storage::File