lib/google/cloud/storage.rb in google-cloud-storage-1.20.0 vs lib/google/cloud/storage.rb in google-cloud-storage-1.21.0
- old
+ new
@@ -54,10 +54,12 @@
#
# * `https://www.googleapis.com/auth/devstorage.full_control`
# @param [Integer] retries Number of times to retry requests on server
# error. The default value is `3`. Optional.
# @param [Integer] timeout Default timeout to use in requests. Optional.
+ # @param [String] endpoint Override of the endpoint host name. Optional.
+ # If the param is nil, uses the default endpoint.
# @param [String] project Alias for the `project_id` argument. Deprecated.
# @param [String] keyfile Alias for the `credentials` argument.
# Deprecated.
#
# @return [Google::Cloud::Storage::Project]
@@ -72,30 +74,28 @@
#
# bucket = storage.bucket "my-bucket"
# file = bucket.file "path/to/my-file.ext"
#
def self.new project_id: nil, credentials: nil, scope: nil, retries: nil,
- timeout: nil, project: nil, keyfile: nil
- project_id ||= (project || default_project_id)
+ timeout: nil, endpoint: nil, project: nil, keyfile: nil
scope ||= configure.scope
retries ||= configure.retries
timeout ||= configure.timeout
+ endpoint ||= configure.endpoint
credentials ||= (keyfile || default_credentials(scope: scope))
unless credentials.is_a? Google::Auth::Credentials
credentials = Storage::Credentials.new credentials, scope: scope
end
- if credentials.respond_to? :project_id
- project_id ||= credentials.project_id
- end
- project_id = project_id.to_s # Always cast to a string
+ project_id = resolve_project_id(project_id || project, credentials)
raise ArgumentError, "project_id is missing" if project_id.empty?
Storage::Project.new(
Storage::Service.new(
- project_id, credentials, retries: retries, timeout: timeout
+ project_id, credentials,
+ retries: retries, timeout: timeout, host: endpoint
)
)
end
##
@@ -103,10 +103,12 @@
# from the Storage service. Each call creates a new connection.
#
# @param [Integer] retries Number of times to retry requests on server
# error. The default value is `3`. Optional.
# @param [Integer] timeout Default timeout to use in requests. Optional.
+ # @param [String] endpoint Override of the endpoint host name. Optional.
+ # If the param is nil, uses the default endpoint.
#
# @return [Google::Cloud::Storage::Project]
#
# @example Use `skip_lookup` to avoid retrieving non-public metadata:
# require "google/cloud/storage"
@@ -118,13 +120,15 @@
#
# downloaded = file.download
# downloaded.rewind
# downloaded.read #=> "Hello world!"
#
- def self.anonymous retries: nil, timeout: nil
+ def self.anonymous retries: nil, timeout: nil, endpoint: nil
Storage::Project.new(
- Storage::Service.new(nil, nil, retries: retries, timeout: timeout)
+ Storage::Service.new(
+ nil, nil, retries: retries, timeout: timeout, host: endpoint
+ )
)
end
##
# Configure the Google Cloud Storage library.
@@ -135,10 +139,12 @@
# parameter `project` is considered deprecated, but may also be used.)
# * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
# the keyfile as a String, the contents of the keyfile as a Hash, or a
# Google::Auth::Credentials object. (See {Storage::Credentials}) (The
# parameter `keyfile` is considered deprecated, but may also be used.)
+ # * `endpoint` - (String) Override of the endpoint host name, or `nil`
+ # to use the default endpoint.
# * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
# the set of resources and operations that the connection can access.
# * `retries` - (Integer) Number of times to retry requests on server
# error.
# * `timeout` - (Integer) Default timeout to use in requests.
@@ -148,9 +154,19 @@
#
def self.configure
yield Google::Cloud.configure.storage if block_given?
Google::Cloud.configure.storage
+ end
+
+ ##
+ # @private Resolve project.
+ def self.resolve_project_id given_project, credentials
+ project_id = given_project || default_project_id
+ if credentials.respond_to? :project_id
+ project_id ||= credentials.project_id
+ end
+ project_id.to_s # Always cast to a string
end
##
# @private Default project.
def self.default_project_id