in gslib/gcs_json_api.py [0:0]
def __init__(self,
bucket_storage_uri_class,
logger,
status_queue,
provider=None,
credentials=None,
debug=0,
http_headers=None,
trace_token=None,
perf_trace_token=None,
user_project=None):
"""Performs necessary setup for interacting with Google Cloud Storage.
Args:
bucket_storage_uri_class: Unused.
logger: logging.logger for outputting log messages.
status_queue: Queue for relaying status to UI.
provider: Unused. This implementation supports only Google Cloud Storage.
credentials: Credentials to be used for interacting with Google Cloud
Storage.
debug: Debug level for the API implementation (0..3).
http_headers (dict|None): Arbitrary headers to be included in every request.
trace_token: Trace token to pass to the API implementation.
perf_trace_token: Performance trace token to use when making API calls.
user_project: Project to be billed for this request.
"""
# TODO: Plumb host_header for perfdiag / test_perfdiag.
# TODO: Add jitter to apitools' http_wrapper retry mechanism.
super(GcsJsonApi, self).__init__(bucket_storage_uri_class,
logger,
status_queue,
provider='gs',
debug=debug,
http_headers=http_headers,
trace_token=trace_token,
perf_trace_token=perf_trace_token,
user_project=user_project)
self.certs_file = GetCertsFile()
self.http = GetNewHttp()
SetUpJsonCredentialsAndCache(self, logger, credentials=credentials)
# Re-use download and upload connections. This class is only called
# sequentially, but we can share TCP warmed-up connections across calls.
self.download_http = self._GetNewDownloadHttp()
self.upload_http = self._GetNewUploadHttp()
if self.credentials:
self.authorized_download_http = self.credentials.authorize(
self.download_http)
self.authorized_upload_http = self.credentials.authorize(self.upload_http)
else:
self.authorized_download_http = self.download_http
self.authorized_upload_http = self.upload_http
if isP12Credentials(self.credentials):
WrapDownloadHttpRequest(self.authorized_download_http.http)
WrapUploadHttpRequest(self.authorized_upload_http.http)
else:
WrapDownloadHttpRequest(self.authorized_download_http)
WrapUploadHttpRequest(self.authorized_upload_http)
self.http_base = 'https://'
gs_json_host = config.get('Credentials', 'gs_json_host', None)
if (context_config.get_context_config() and
context_config.get_context_config().use_client_certificate):
if gs_json_host:
raise ArgumentException(
'"use_client_certificate" is enabled, which sets gsutil to use the'
' host {}. However, a custom host was set using'
' "gs_json_host": {}. Please set "use_client_certificate" to'
' "False" or comment out the "gs_json_host" line in the Boto'
' config.'.format(MTLS_HOST, gs_json_host))
self.host_base = MTLS_HOST
else:
self.host_base = gs_json_host or DEFAULT_HOST
if not gs_json_host:
gs_host = config.get('Credentials', 'gs_host', None)
if gs_host:
raise ArgumentException(
'JSON API is selected but gs_json_host is not configured, '
'while gs_host is configured to %s. Please also configure '
'gs_json_host and gs_json_port to match your desired endpoint.' %
gs_host)
gs_json_port = config.get('Credentials', 'gs_json_port', None)
if not gs_json_port:
gs_port = config.get('Credentials', 'gs_port', None)
if gs_port:
raise ArgumentException(
'JSON API is selected but gs_json_port is not configured, '
'while gs_port is configured to %s. Please also configure '
'gs_json_host and gs_json_port to match your desired endpoint.' %
gs_port)
self.host_port = ''
else:
self.host_port = ':' + config.get('Credentials', 'gs_json_port')
self.api_version = GetGcsJsonApiVersion()
self.url_base = (self.http_base + self.host_base + self.host_port + '/' +
'storage/' + self.api_version + '/')
self.global_params = apitools_messages.StandardQueryParameters(
trace='token:%s' % trace_token) if trace_token else None
additional_http_headers = {}
gs_json_host_header = config.get('Credentials', 'gs_json_host_header', None)
if gs_json_host_header and gs_json_host:
additional_http_headers['Host'] = gs_json_host_header
self._UpdateHeaders(additional_http_headers)
log_request = (debug >= 3)
log_response = (debug >= 3)
self.api_client = apitools_client.StorageV1(
url=self.url_base,
http=self.http,
log_request=log_request,
log_response=log_response,
credentials=self.credentials,
version=self.api_version,
default_global_params=self.global_params,
additional_http_headers=additional_http_headers)
self.max_retry_wait = GetMaxRetryDelay()
self.api_client.max_retry_wait = self.max_retry_wait
self.num_retries = GetNumRetries()
self.api_client.num_retries = self.num_retries
self.api_client.retry_func = LogAndHandleRetries(
status_queue=self.status_queue)
self.api_client.overwrite_transfer_urls_with_client_base = True
if isinstance(self.credentials, NoOpCredentials):
# This API key is not secret and is used to identify gsutil during
# anonymous requests.
self.api_client.AddGlobalParam('key',
'AIzaSyDnacJHrKma0048b13sh8cgxNUwulubmJM')