def _UploadFileToGeneratedUrl(source, messages, service, function_ref): """Upload function source to URL generated by API.""" url = _GetUploadUrl(messages, service, function_ref) upload = transfer.Upload.FromFile(source, mime_type='application/zip') try: upload_request = http_wrapper.Request( url, http_method='PUT', headers={ 'content-type': 'application/zip', # Magic header, request will fail without it. # Not documented at the moment this comment was being written. 'x-goog-content-length-range': '0,104857600', 'Content-Length': '{0:d}'.format(upload.total_size) }) upload_request.body = upload.stream.read() finally: upload.stream.close() response = http_wrapper.MakeRequest( transports.GetApitoolsTransport(), upload_request, retry_func=lambda ra: _UploadFileToGeneratedUrlRetryFunc( # pylint: disable=g-long-lambda upload.retry_func, ra), check_response_func=lambda r: _UploadFileToGeneratedUrlCheckResponse( # pylint: disable=g-long-lambda http_wrapper.CheckResponse, r), retries=upload.num_retries) if not _CheckUploadStatus(response.status_code): raise exceptions.FunctionsError( 'Failed to upload the function source code to signed url: {url}. ' 'Status: [{code}:{detail}]'.format(url=url, code=response.status_code, detail=response.content)) return url
def Http(): """Gets an transport client for use with containerregistry. For now, this just calls into GetApitoolsTransport, but if we find that implementation does not satisfy our needs, we may need to fork it. This small amount of indirection will make that change a bit cleaner. Returns: 1. A httplib2.Http-like object backed by httplib2 or requests. """ return transports.GetApitoolsTransport()
def _UploadToGeneratedUrl(zip_file_path, url): """Uploads a ZIP file to a signed Cloud Storage URL. Args: zip_file_path: str, the path to the ZIP file url: str, the signed Cloud Storage URL """ upload = transfer.Upload.FromFile(zip_file_path, mime_type=_ZIP_MIME_TYPE) try: request = http_wrapper.Request( url, http_method='PUT', headers={'content-type': upload.mime_type}) request.body = upload.stream.read() upload.stream.close() response = http_wrapper.MakeRequest(transports.GetApitoolsTransport(), request) finally: upload.stream.close() if response.status_code // 100 != 2: raise exceptions.FunctionsError(_SIGNED_URL_UPLOAD_ERROR_MESSSAGE)