René's URL Explorer Experiment


Title: Vision: Google Cloud Storage Client project=None bootstrapping problem · Issue #4239 · googleapis/google-cloud-python · GitHub

Open Graph Title: Vision: Google Cloud Storage Client project=None bootstrapping problem · Issue #4239 · googleapis/google-cloud-python

X Title: Vision: Google Cloud Storage Client project=None bootstrapping problem · Issue #4239 · googleapis/google-cloud-python

Description: Hello, In using the Google Cloud API's, I noticed a small discrepancy in the documentation's expected behavior, and the actual behavior. The documentation and Google cloud examples code typically assumes that credentials should be create...

Open Graph Description: Hello, In using the Google Cloud API's, I noticed a small discrepancy in the documentation's expected behavior, and the actual behavior. The documentation and Google cloud examples code typically a...

X Description: Hello, In using the Google Cloud API's, I noticed a small discrepancy in the documentation's expected behavior, and the actual behavior. The documentation and Google cloud examples code typ...

Opengraph URL: https://github.com/googleapis/google-cloud-python/issues/4239

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Vision: Google Cloud Storage Client project=None bootstrapping problem","articleBody":"Hello,\r\n\r\nIn using the Google Cloud API's, I noticed a small discrepancy in the documentation's expected behavior, and the actual behavior. \r\nThe documentation and Google cloud examples code typically assumes that credentials should be created using a JSON key store saved on persistent storage. However, this isn't always an option, as with deployments using ephemeral storage. Luckily, a Credentials object can be created directly from a dictionary, which houses the same mapping that the JSON keystore would hold. This is the procedure that I use in my project (the specifics of which, is shown below).\r\n\r\n\r\nThe [Storage.Client documentation](https://googlecloudplatform.github.io/google-cloud-python/latest/storage/client.html#google.cloud.storage.client.Client) suggests that the `project` parameter is optional, and thus may be None. We found, however, that when it is not specified, an error is thrown, as a result of a sort of bootstrapping problem in which the Google Cloud APIs check to see if the project is None, and if so, attempt to create it (which is impossible, without first having the keys).\r\nTo elaborate:\r\n\r\n1. When we make a call to Storage.Client(), we step into [storage/google/cloud/storage/client.py](https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/). \r\n2. This class calls `super(Client, self).__init__(project=project, credentials=credentials, _http=_http)`, which takes us to [core/google/cloud/client.py](https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/core/google/cloud/client.py) .\r\n3. This calls `_ClientProjectMixin.__init__(self, project=project)`, where we see the following call:\r\n```\r\n        project = self._determine_default(project)\r\n```\r\n4. Following this call, we see:\r\n```\r\n    if project is None:\r\n        _, project = google.auth.default()\r\n```\r\n\r\n\r\nSo, there is an attempt to infer the project parameter based on the environment credentials. However, as discussed above, we are using a dictionary-created Credentials object as our credentials,  so no such credentials exist in the environment. **This is a problem because the documentation claims that project is optional, when, in fact, it is not if the project is not stored in the environment.**\r\n\r\nNote, however, that this seems to be a problem specific with Storage.Client(), and not other API clients. Trying this procedure (see the code below) for `google.cloud.vision.ImageAnnotatorClient` seems to work perfectly fine with just a credentials object, without the project argument. \r\n\r\n\r\n----\r\n\r\n\r\n\r\n1. **OS:** Ubuntu 16.04\r\n2. **Python Version:** Python 2.7.10\r\n3. **Google APIs:**\r\ngoogle-auth==1.1.1\r\ngoogle-cloud-core==0.27.1\r\ngoogle-cloud-storage==1.4.0\r\ngoogle-cloud-vision==0.27.0\r\ngoogle-gax==0.15.15\r\ngoogle-resumable-media==0.2.3\r\ngoogleapis-common-protos==1.5.3\r\noauth2client==4.1.2\r\n\r\n\r\n4. **Stacktrace:**\r\n\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"storage_client.py\", line 23, in \u003cmodule\u003e\r\n    client = _get_client()\r\n  File \"storage_client.py\", line 18, in _get_client\r\n    client = storage.Client(credentials=creds)\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/cloud/storage/client.py\", line 59, in __init__\r\n    _http=_http)\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/cloud/client.py\", line 211, in __init__\r\n    _ClientProjectMixin.__init__(self, project=project)\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/cloud/client.py\", line 165, in __init__\r\n    project = self._determine_default(project)\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/cloud/client.py\", line 178, in _determine_default\r\n    return _determine_default_project(project)\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/cloud/_helpers.py\", line 179, in _determine_default_project\r\n    _, project = google.auth.default()\r\n  File \"/Users/admin/testproject/venv/lib/python2.7/site-packages/google/auth/_default.py\", line 286, in default\r\n    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)\r\ngoogle.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or\r\nexplicitly create credential and re-run the application. For more\r\ninformation, please see\r\nhttps://developers.google.com/accounts/docs/application-default-credentials.\r\n\r\n```\r\n\r\n\r\n5. **Steps to reproduce:**\r\n\r\n\r\n\r\n- So, create an OAuth2 credentials object from a python dictionary (let's call it `ketfile_dict` using `google.oauth2.service_account.Credentials.from_service_account_info(keyfile_dict)`.\r\n\r\n- Create an instance of a storage client using `google.cloud.storage.Client(credentials=creds)`\r\n\r\n- Although the documentation states that the \"project\" argument is optional, the API will in fact throw an error when no project argument is presented.\r\n\r\n6. Code example\r\n\r\nAssume that in the same directory, you create a file, `constants.py`, with the following fields defined from the appropriate fields of the service account key JSON.\r\n\r\nconstants.GCP_SCOPE\r\nconstants.GCP_TYPE\r\nconstants.GCP_CLIENT_EMAIL\r\nconstants.GCP_PRIVATE_KEY\r\nconstants.GCP_PRIVATE_KEY_ID\r\nconstants.GCP_CLIENT_ID\r\nconstants.GCP_TOKEN_URI\r\nconstants.GCP_PROJECT_ID\r\n\r\n\r\n\r\n```\r\nfrom google.cloud import storage\r\nfrom google.oauth2 import service_account\r\nimport constants\r\n\r\n\r\n# Return an Oauth2 credentials instance\r\ndef get_credentials(with_keyfile=False):\r\n    keyfile_dict = _get_keyfile_dict()\r\n    scope = [constants.GCP_SCOPE]\r\n    creds = service_account.Credentials.from_service_account_info(keyfile_dict).with_scopes(scope)\r\n\r\n    if not with_keyfile:\r\n        return creds\r\n    else:\r\n        # Return creds and the keyfile.\r\n        return creds, keyfile_dict\r\n\r\n\r\n# Construct a dictionary of the required key:value pairs.\r\ndef _get_keyfile_dict():\r\n    keyfile_dict = {}\r\n    keyfile_dict['type'] = constants.GCP_TYPE\r\n    keyfile_dict['client_email'] = constants.GCP_CLIENT_EMAIL\r\n    keyfile_dict['private_key'] = constants.GCP_PRIVATE_KEY\r\n    keyfile_dict['private_key_id'] = constants.GCP_PRIVATE_KEY_ID\r\n    keyfile_dict['client_id'] = constants.GCP_CLIENT_ID\r\n    keyfile_dict['token_uri'] = constants.GCP_TOKEN_URI\r\n    keyfile_dict['project_id'] = constants.GCP_PROJECT_ID\r\n    return keyfile_dict\r\n\r\n\r\ndef _get_client():\r\n    creds, keyfile_dict = get_credentials(with_keyfile=True)\r\n    project = keyfile_dict['project_id']\r\n    #client = storage.Client(project=project, credentials=creds) # This WILL work because a project is passed in.\r\n    #client = vision.ImageAnnotatorClient(credentials=creds) #Note that the Vision client will work without a project argument\r\n    client = storage.Client(credentials=creds) # This will throw an error because project=None.\r\n    return client\r\n\r\n\r\n# Instantiates a client\r\nclient = _get_client()\r\n```\r\n\r\n\r\nLet me know if you have any other questions. :)\r\n\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/safi-manar","@type":"Person","name":"safi-manar"},"datePublished":"2017-10-23T09:04:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/4239/google-cloud-python/issues/4239"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:c132814f-470d-6661-9fb4-057250297d08
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idEB94:18C74D:1117C39:1757A83:6A4D6803
html-safe-nonce834c03102a96625af411db5cd6707b30e03acaa3482e618c05d347510902d5f1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjk0OjE4Qzc0RDoxMTE3QzM5OjE3NTdBODM6NkE0RDY4MDMiLCJ2aXNpdG9yX2lkIjoiNjUzMDgxNjM4MDMyMDQ0MjM3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb231c706eb4d2317206736b4046c735978bb74e7973fce3504d053bda2b83f1d
hovercard-subject-tagissue:267592147
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-cloud-python/4239/issue_layout
twitter:imagehttps://opengraph.githubassets.com/37a0f30bacf286e0ce7553a619eb2e332c7bafb088bac0def989328c8fa1b34e/googleapis/google-cloud-python/issues/4239
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/37a0f30bacf286e0ce7553a619eb2e332c7bafb088bac0def989328c8fa1b34e/googleapis/google-cloud-python/issues/4239
og:image:altHello, In using the Google Cloud API's, I noticed a small discrepancy in the documentation's expected behavior, and the actual behavior. The documentation and Google cloud examples code typically a...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesafi-manar
hostnamegithub.com
expected-hostnamegithub.com
None7db3950e3f59095e20a9789440c3dd554efc62d434882ead06ae05b5815605b9
turbo-cache-controlno-preview
go-importgithub.com/googleapis/google-cloud-python git https://github.com/googleapis/google-cloud-python.git
octolytics-dimension-user_id16785467
octolytics-dimension-user_logingoogleapis
octolytics-dimension-repository_id16316451
octolytics-dimension-repository_nwogoogleapis/google-cloud-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id16316451
octolytics-dimension-repository_network_root_nwogoogleapis/google-cloud-python
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasecd8f6a71db61d6012a0cbb134be844fdbe9b8a45
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/googleapis/google-cloud-python/issues/4239#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-cloud-python%2Fissues%2F4239
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-cloud-python%2Fissues%2F4239
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=googleapis%2Fgoogle-cloud-python
Reloadhttps://github.com/googleapis/google-cloud-python/issues/4239
Reloadhttps://github.com/googleapis/google-cloud-python/issues/4239
Reloadhttps://github.com/googleapis/google-cloud-python/issues/4239
Please reload this pagehttps://github.com/googleapis/google-cloud-python/issues/4239
googleapis https://github.com/googleapis
google-cloud-pythonhttps://github.com/googleapis/google-cloud-python
Notifications https://github.com/login?return_to=%2Fgoogleapis%2Fgoogle-cloud-python
Fork 1.7k https://github.com/login?return_to=%2Fgoogleapis%2Fgoogle-cloud-python
Star 5.3k https://github.com/login?return_to=%2Fgoogleapis%2Fgoogle-cloud-python
Code https://github.com/googleapis/google-cloud-python
Issues 438 https://github.com/googleapis/google-cloud-python/issues
Pull requests 129 https://github.com/googleapis/google-cloud-python/pulls
Discussions https://github.com/googleapis/google-cloud-python/discussions
Actions https://github.com/googleapis/google-cloud-python/actions
Projects https://github.com/googleapis/google-cloud-python/projects
Security and quality 0 https://github.com/googleapis/google-cloud-python/security
Insights https://github.com/googleapis/google-cloud-python/pulse
Code https://github.com/googleapis/google-cloud-python
Issues https://github.com/googleapis/google-cloud-python/issues
Pull requests https://github.com/googleapis/google-cloud-python/pulls
Discussions https://github.com/googleapis/google-cloud-python/discussions
Actions https://github.com/googleapis/google-cloud-python/actions
Projects https://github.com/googleapis/google-cloud-python/projects
Security and quality https://github.com/googleapis/google-cloud-python/security
Insights https://github.com/googleapis/google-cloud-python/pulse
Vision: Google Cloud Storage Client project=None bootstrapping problemhttps://github.com/googleapis/google-cloud-python/issues/4239#top
https://github.com/tseaver
api: storageIssues related to the Cloud Storage API.https://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22api%3A%20storage%22
authhttps://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22auth%22
priority: p2Moderately-important priority. Fix may not be included in next release.https://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22priority%3A%20p2%22
https://github.com/safi-manar
safi-manarhttps://github.com/safi-manar
on Oct 23, 2017https://github.com/googleapis/google-cloud-python/issues/4239#issue-267592147
Storage.Client documentationhttps://googlecloudplatform.github.io/google-cloud-python/latest/storage/client.html#google.cloud.storage.client.Client
storage/google/cloud/storage/client.pyhttps://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/
core/google/cloud/client.pyhttps://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/core/google/cloud/client.py
tseaverhttps://github.com/tseaver
api: storageIssues related to the Cloud Storage API.https://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22api%3A%20storage%22
authhttps://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22auth%22
priority: p2Moderately-important priority. Fix may not be included in next release.https://github.com/googleapis/google-cloud-python/issues?q=state%3Aopen%20label%3A%22priority%3A%20p2%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.