Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): fetch file and bucket details from url #27322

Merged
49 changes: 49 additions & 0 deletions google-cloud-storage/lib/google/cloud/storage/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,55 @@ 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:
shubhangi-google marked this conversation as resolved.
Show resolved Hide resolved
# require "google/cloud/storage"
# gs_url= "gs://my-todo-app/avatars/heidi.jpeg"
# fileObject=Google::Cloud::Storage::File
shubhangi-google marked this conversation as resolved.
Show resolved Hide resolved
# fileObject.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:
# require "google/cloud/storage"
# gs_url= "gs://my-todo-app/test_sub_folder/heidi.jpeg?params1=test1&params2=test2"
# fileObject=Google::Cloud::Storage::File
# fileObject.from_gs_url(gs_url)
# =>{
# "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
# encoded_url = url.strip.gsub('@','%40')
shubhangi-google marked this conversation as resolved.
Show resolved Hide resolved
parsed_url = URI.parse url
query_params = URI.decode_www_form(parsed_url.query).to_h if parsed_url.query
parsed_url.query = nil
shubhangi-google marked this conversation as resolved.
Show resolved Hide resolved
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! "options" => query_params if query_params
url_items
end


protected

##
Expand Down
13 changes: 13 additions & 0 deletions google-cloud-storage/test/google/cloud/storage/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,19 @@ def file_user_project.sleep *args
end
end

describe "fetch details from gs_url" do
shubhangi-google marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
Loading