Class: Fog::Storage::GoogleJSON::Real
- Inherits:
-
Object
- Object
- Fog::Storage::GoogleJSON::Real
- Includes:
- Google::Shared, GetObjectHttpUrl, GetObjectHttpsUrl, Utils
- Defined in:
- lib/fog/storage/google_json/real.rb,
lib/fog/storage/google_json/requests/get_bucket.rb,
lib/fog/storage/google_json/requests/get_object.rb,
lib/fog/storage/google_json/requests/put_bucket.rb,
lib/fog/storage/google_json/requests/put_object.rb,
lib/fog/storage/google_json/requests/copy_object.rb,
lib/fog/storage/google_json/requests/list_buckets.rb,
lib/fog/storage/google_json/requests/list_objects.rb,
lib/fog/storage/google_json/requests/delete_bucket.rb,
lib/fog/storage/google_json/requests/delete_object.rb,
lib/fog/storage/google_json/requests/get_bucket_acl.rb,
lib/fog/storage/google_json/requests/get_object_acl.rb,
lib/fog/storage/google_json/requests/get_object_url.rb,
lib/fog/storage/google_json/requests/put_bucket_acl.rb,
lib/fog/storage/google_json/requests/put_object_acl.rb,
lib/fog/storage/google_json/requests/put_object_url.rb,
lib/fog/storage/google_json/requests/list_bucket_acl.rb,
lib/fog/storage/google_json/requests/list_object_acl.rb,
lib/fog/storage/google_json/requests/delete_object_url.rb,
lib/fog/storage/google_json/requests/get_object_http_url.rb,
lib/fog/storage/google_json/requests/get_object_metadata.rb,
lib/fog/storage/google_json/requests/get_object_https_url.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#storage_json ⇒ Object
readonly
Returns the value of attribute storage_json.
Attributes included from Google::Shared
#api_url, #api_version, #project
Instance Method Summary collapse
-
#copy_object(source_bucket, source_object, target_bucket, target_object, options = {}) ⇒ Google::Apis::StorageV1::Object
Copy an object from one Google Storage bucket to another.
-
#delete_bucket(bucket_name) ⇒ Object
Delete an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/buckets/delete.
-
#delete_object(bucket_name, object_name) ⇒ Object
Delete an object from Google Storage cloud.google.com/storage/docs/json_api/v1/objects/delete.
-
#delete_object_url(bucket_name, object_name, expires) ⇒ String
Get an expiring object url from Google Storage for deleting an object cloud.google.com/storage/docs/access-control#Signed-URLs.
-
#get_bucket(bucket_name, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil) ⇒ Google::Apis::StorageV1::Bucket
List information about objects in an Google Storage bucket # cloud.google.com/storage/docs/json_api/v1/buckets#resource.
-
#get_bucket_acl(bucket_name, entity) ⇒ Google::Apis::StorageV1::BucketAccessControls
Get access control list entry for an Google Storage bucket.
-
#get_object(bucket_name, object_name, generation: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil, **options, &_block) ⇒ Hash
Get an object from Google Storage.
-
#get_object_acl(bucket_name, object_name, entity, generation: nil) ⇒ Google::Apis::StorageV1::ObjectAccessControls
Get access control list for an Google Storage object cloud.google.com/storage/docs/json_api/v1/objectAccessControls/get.
-
#get_object_metadata(bucket_name, object_name, options = {}) ⇒ Google::Apis::StorageV1::Object
Fetch metadata for an object in Google Storage.
-
#get_object_url(bucket_name, object_name, expires, options = {}) ⇒ Object
Get an expiring object url from GCS Deprecated, redirects to get_object_https_url.rb.
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
-
#list_bucket_acl(bucket_name) ⇒ Google::Apis::StorageV1::BucketAccessControls
Get access control list for an Google Storage bucket.
-
#list_buckets(max_results: nil, page_token: nil, prefix: nil, projection: nil) ⇒ Google::Apis::StorageV1::Buckets
Retrieves a list of buckets for a given project cloud.google.com/storage/docs/json_api/v1/buckets/list.
-
#list_object_acl(bucket_name, object_name, generation: nil) ⇒ Google::Apis::StorageV1::ObjectAccessControls
List access control list for an Google Storage object cloud.google.com/storage/docs/json_api/v1/objectAccessControls/get.
-
#list_objects(bucket, options = {}) ⇒ Google::Apis::StorageV1::Objects
Lists objects in a bucket matching some criteria.
-
#put_bucket(bucket_name, predefined_acl: nil, predefined_default_object_acl: nil, **options) ⇒ Google::Apis::StorageV1::Bucket
Create a Google Storage bucket.
-
#put_bucket_acl(bucket_name, acl = {}) ⇒ Google::Apis::StorageV1::BucketAccessControl
Change access control list for an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/insert.
-
#put_object(bucket_name, object_name, data, content_encoding: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, kms_key_name: nil, predefined_acl: nil, **options) ⇒ Google::Apis::StorageV1::Object
Create an object in an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/objects/insert.
-
#put_object_acl(bucket_name, object_name, acl) ⇒ Google::Apis::StorageV1::ObjectAccessControl
Change access control list for an Google Storage object.
-
#put_object_url(bucket_name, object_name, expires, headers = {}) ⇒ String
Get an expiring object url from Google Storage for putting an object cloud.google.com/storage/docs/access-control#Signed-URLs.
- #signature(params) ⇒ Object
Methods included from GetObjectHttpsUrl
Methods included from GetObjectHttpUrl
Methods included from Google::Shared
#apply_client_options, #build_excon_response, #initialize_auth, #initialize_google_client, #request, #shared_initialize
Methods included from Utils
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fog/storage/google_json/real.rb', line 11 def initialize( = {}) shared_initialize([:google_project], GOOGLE_STORAGE_JSON_API_VERSION, GOOGLE_STORAGE_JSON_BASE_URL) @options = .dup [:google_api_scope_url] = GOOGLE_STORAGE_JSON_API_SCOPE_URLS.join(" ") @host = [:host] || "storage.googleapis.com" # TODO(temikus): Do we even need this client? @client = initialize_google_client() @storage_json = ::Google::Apis::StorageV1::StorageService.new (@storage_json, ) @storage_json..open_timeout_sec = [:open_timeout_sec] if [:open_timeout_sec] @storage_json..read_timeout_sec = [:read_timeout_sec] if [:read_timeout_sec] @storage_json..send_timeout_sec = [:send_timeout_sec] if [:send_timeout_sec] end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
8 9 10 |
# File 'lib/fog/storage/google_json/real.rb', line 8 def client @client end |
#storage_json ⇒ Object (readonly)
Returns the value of attribute storage_json.
9 10 11 |
# File 'lib/fog/storage/google_json/real.rb', line 9 def storage_json @storage_json end |
Instance Method Details
#copy_object(source_bucket, source_object, target_bucket, target_object, options = {}) ⇒ Google::Apis::StorageV1::Object
Copy an object from one Google Storage bucket to another
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fog/storage/google_json/requests/copy_object.rb', line 14 def copy_object(source_bucket, source_object, target_bucket, target_object, = {}) = ::Google::Apis::RequestOptions.default.merge() object = ::Google::Apis::StorageV1::Object.new(**) @storage_json.copy_object(source_bucket, source_object, target_bucket, target_object, object, options: , **filter_keyword_args()) end |
#delete_bucket(bucket_name) ⇒ Object
Delete an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/buckets/delete
9 10 11 |
# File 'lib/fog/storage/google_json/requests/delete_bucket.rb', line 9 def delete_bucket(bucket_name) @storage_json.delete_bucket(bucket_name) end |
#delete_object(bucket_name, object_name) ⇒ Object
Delete an object from Google Storage cloud.google.com/storage/docs/json_api/v1/objects/delete
10 11 12 |
# File 'lib/fog/storage/google_json/requests/delete_object.rb', line 10 def delete_object(bucket_name, object_name) @storage_json.delete_object(bucket_name, object_name) end |
#delete_object_url(bucket_name, object_name, expires) ⇒ String
Get an expiring object url from Google Storage for deleting an object cloud.google.com/storage/docs/access-control#Signed-URLs
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fog/storage/google_json/requests/delete_object_url.rb', line 13 def delete_object_url(bucket_name, object_name, expires) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name https_url({ :headers => {}, :host => @host, :method => "DELETE", :path => "#{bucket_name}/#{object_name}" }, expires) end |
#get_bucket(bucket_name, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil) ⇒ Google::Apis::StorageV1::Bucket
List information about objects in an Google Storage bucket # cloud.google.com/storage/docs/json_api/v1/buckets#resource
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/storage/google_json/requests/get_bucket.rb', line 19 def get_bucket(bucket_name, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil) raise ArgumentError.new("bucket_name is required") unless bucket_name @storage_json.get_bucket( bucket_name, :if_metageneration_match => , :if_metageneration_not_match => , :projection => projection ) end |
#get_bucket_acl(bucket_name, entity) ⇒ Google::Apis::StorageV1::BucketAccessControls
Get access control list entry for an Google Storage bucket
15 16 17 18 19 20 |
# File 'lib/fog/storage/google_json/requests/get_bucket_acl.rb', line 15 def get_bucket_acl(bucket_name, entity) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("entity is required") unless entity @storage_json.get_bucket_access_control(bucket_name, entity) end |
#get_object(bucket_name, object_name, generation: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil, **options, &_block) ⇒ Hash
Get an object from Google Storage
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fog/storage/google_json/requests/get_object.rb', line 30 def get_object(bucket_name, object_name, generation: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, projection: nil, **, &_block) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name buf = Tempfile.new("fog-google-storage-temp") buf.binmode buf.unlink # Two requests are necessary, first for metadata, then for content. # google-api-ruby-client doesn't allow fetching both metadata and content = ::Google::Apis::RequestOptions.default.merge() all_opts = { :generation => generation, :if_generation_match => if_generation_match, :if_generation_not_match => if_generation_not_match, :if_metageneration_match => , :if_metageneration_not_match => , :projection => projection, :options => } object = @storage_json.get_object(bucket_name, object_name, **all_opts).to_h @storage_json.get_object( bucket_name, object_name, **all_opts.merge(:download_dest => buf) ) buf.seek(0) if block_given? yield buf.read, nil, nil else object[:body] = buf.read end object ensure buf.close! rescue nil end |
#get_object_acl(bucket_name, object_name, entity, generation: nil) ⇒ Google::Apis::StorageV1::ObjectAccessControls
Get access control list for an Google Storage object cloud.google.com/storage/docs/json_api/v1/objectAccessControls/get
15 16 17 18 19 20 21 22 23 |
# File 'lib/fog/storage/google_json/requests/get_object_acl.rb', line 15 def get_object_acl(bucket_name, object_name, entity, generation: nil) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name @storage_json.get_object_access_control( bucket_name, object_name, entity, :generation => generation ) end |
#get_object_metadata(bucket_name, object_name, options = {}) ⇒ Google::Apis::StorageV1::Object
Fetch metadata for an object in Google Storage
13 14 15 16 17 18 19 20 |
# File 'lib/fog/storage/google_json/requests/get_object_metadata.rb', line 13 def (bucket_name, object_name, = {}) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name = ::Google::Apis::RequestOptions.default.merge() @storage_json.get_object(bucket_name, object_name, :options => ) end |
#get_object_url(bucket_name, object_name, expires, options = {}) ⇒ Object
Get an expiring object url from GCS Deprecated, redirects to get_object_https_url.rb
7 8 9 10 |
# File 'lib/fog/storage/google_json/requests/get_object_url.rb', line 7 def get_object_url(bucket_name, object_name, expires, = {}) Fog::Logger.deprecation("Fog::Storage::Google => #get_object_url is deprecated, use #get_object_https_url instead[/] [light_black](#{caller(0..0)})") get_object_https_url(bucket_name, object_name, expires, **) end |
#list_bucket_acl(bucket_name) ⇒ Google::Apis::StorageV1::BucketAccessControls
Get access control list for an Google Storage bucket
10 11 12 13 14 |
# File 'lib/fog/storage/google_json/requests/list_bucket_acl.rb', line 10 def list_bucket_acl(bucket_name) raise ArgumentError.new("bucket_name is required") unless bucket_name @storage_json.list_bucket_access_controls(bucket_name) end |
#list_buckets(max_results: nil, page_token: nil, prefix: nil, projection: nil) ⇒ Google::Apis::StorageV1::Buckets
Retrieves a list of buckets for a given project cloud.google.com/storage/docs/json_api/v1/buckets/list
TODO: check if very large lists require working with nextPageToken
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fog/storage/google_json/requests/list_buckets.rb', line 10 def list_buckets(max_results: nil, page_token: nil, prefix: nil, projection: nil) @storage_json.list_buckets( @project, max_results: max_results, page_token: page_token, prefix: prefix, projection: projection ) end |
#list_object_acl(bucket_name, object_name, generation: nil) ⇒ Google::Apis::StorageV1::ObjectAccessControls
List access control list for an Google Storage object cloud.google.com/storage/docs/json_api/v1/objectAccessControls/get
11 12 13 14 15 16 17 |
# File 'lib/fog/storage/google_json/requests/list_object_acl.rb', line 11 def list_object_acl(bucket_name, object_name, generation: nil) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name @storage_json.list_object_access_controls(bucket_name, object_name, :generation => generation) end |
#list_objects(bucket, options = {}) ⇒ Google::Apis::StorageV1::Objects
Lists objects in a bucket matching some criteria.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fog/storage/google_json/requests/list_objects.rb', line 22 def list_objects(bucket, = {}) allowed_opts = %i( delimiter max_results page_token prefix projection versions ) @storage_json.list_objects( bucket, **.select { |k, _| allowed_opts.include? k } ) end |
#put_bucket(bucket_name, predefined_acl: nil, predefined_default_object_acl: nil, **options) ⇒ Google::Apis::StorageV1::Bucket
Create a Google Storage bucket
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fog/storage/google_json/requests/put_bucket.rb', line 17 def put_bucket(bucket_name, predefined_acl: nil, predefined_default_object_acl: nil, **) bucket = ::Google::Apis::StorageV1::Bucket.new( **.merge(:name => bucket_name) ) @storage_json.insert_bucket( @project, bucket, :predefined_acl => predefined_acl, :predefined_default_object_acl => predefined_default_object_acl ) end |
#put_bucket_acl(bucket_name, acl = {}) ⇒ Google::Apis::StorageV1::BucketAccessControl
Change access control list for an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/bucketAccessControls/insert
11 12 13 14 15 16 17 |
# File 'lib/fog/storage/google_json/requests/put_bucket_acl.rb', line 11 def put_bucket_acl(bucket_name, acl = {}) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("acl is required") unless acl acl_update = ::Google::Apis::StorageV1::BucketAccessControl.new(**acl) @storage_json.insert_bucket_access_control(bucket_name, acl_update) end |
#put_object(bucket_name, object_name, data, content_encoding: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, kms_key_name: nil, predefined_acl: nil, **options) ⇒ Google::Apis::StorageV1::Object
Create an object in an Google Storage bucket cloud.google.com/storage/docs/json_api/v1/objects/insert
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fog/storage/google_json/requests/put_object.rb', line 40 def put_object(bucket_name, object_name, data, content_encoding: nil, if_generation_match: nil, if_generation_not_match: nil, if_metageneration_match: nil, if_metageneration_not_match: nil, kms_key_name: nil, predefined_acl: nil, **) data, = normalize_data(data, ) object_config = ::Google::Apis::StorageV1::Object.new( **.merge(:name => object_name) ) @storage_json.insert_object( bucket_name, object_config, :content_encoding => content_encoding, :if_generation_match => if_generation_match, :if_generation_not_match => if_generation_not_match, :if_metageneration_match => , :if_metageneration_not_match => , :kms_key_name => kms_key_name, :predefined_acl => predefined_acl, :options => ::Google::Apis::RequestOptions.default.merge(), # see https://developers.google.com/api-client-library/ruby/guide/media_upload :content_type => [:content_type], :upload_source => data ) end |
#put_object_acl(bucket_name, object_name, acl) ⇒ Google::Apis::StorageV1::ObjectAccessControl
Change access control list for an Google Storage object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fog/storage/google_json/requests/put_object_acl.rb', line 13 def put_object_acl(bucket_name, object_name, acl) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name raise ArgumentError.new("acl is required") unless acl acl_object = ::Google::Apis::StorageV1::ObjectAccessControl.new(**acl) @storage_json.insert_object_access_control( bucket_name, object_name, acl_object ) end |
#put_object_url(bucket_name, object_name, expires, headers = {}) ⇒ String
Get an expiring object url from Google Storage for putting an object cloud.google.com/storage/docs/access-control#Signed-URLs
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fog/storage/google_json/requests/put_object_url.rb', line 16 def put_object_url(bucket_name, object_name, expires, headers = {}) raise ArgumentError.new("bucket_name is required") unless bucket_name raise ArgumentError.new("object_name is required") unless object_name https_url({ :headers => headers, :host => @host, :method => "PUT", :path => "#{bucket_name}/#{object_name}" }, expires) end |
#signature(params) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fog/storage/google_json/real.rb', line 28 def signature(params) string_to_sign = <<-DATA #{params[:method]} #{params[:headers]['Content-MD5']} #{params[:headers]['Content-Type']} #{params[:headers]['Date']} DATA google_headers = {} canonical_google_headers = "" params[:headers].each do |key, value| google_headers[key] = value if key[0..6] == "x-goog-" end google_headers = google_headers.sort_by { |a| a[0] } google_headers.each do |key, value| canonical_google_headers << "#{key}:#{value}\n" end string_to_sign << canonical_google_headers.to_s canonical_resource = "/" if subdomain = params.delete(:subdomain) canonical_resource << "#{CGI.escape(subdomain).downcase}/" end canonical_resource << params[:path].to_s canonical_resource << "?" (params[:query] || {}).each_key do |key| if %w(acl cors location logging requestPayment versions versioning).include?(key) canonical_resource << "#{key}&" end end canonical_resource.chop! string_to_sign << canonical_resource.to_s # TODO(temikus): make signer configurable or add ability to supply your own via lambda if !@storage_json..signing_key.nil? signed_string = default_signer(string_to_sign) else # If client doesn't contain signing key attempt to auth via IAM SignBlob API signed_string = iam_signer(string_to_sign) end Base64.encode64(signed_string).chomp! end |