allow access token to be passed in to `gsutil cp`
See original GitHub issueThe current access token can be obtained by doing a gcloud auth print-access-token
. That works for service accounts too. It would be useful if the access token can be passed in for commands, especially gsutil cp
.
As a workaround, I wrote this script to download files:
#!/usr/bin/env python
# derived from https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/api/crud_object.py
import argparse
import json
import io
from googleapiclient import discovery
from googleapiclient import http
from oauth2client.client import GoogleCredentials
from oauth2client.client import AccessTokenCredentials
def main(bucket, filename, accesstoken):
service = create_service(accesstoken)
with io.FileIO(filename, mode='wb') as outfile:
get_object(service, bucket, filename, outfile)
def create_service(accesstoken):
# print("accesstoken: %s" % accesstoken)
if not accesstoken:
credentials = GoogleCredentials.get_application_default()
else:
credentials = AccessTokenCredentials(accesstoken, 'my-user-agent/1.0')
return discovery.build('storage', 'v1', credentials=credentials)
def get_object(service, bucket, filename, outfile):
print("gsutil cp gs://%s/%s %s" % (bucket, filename, filename))
req = service.objects().get_media(bucket=bucket, object=filename)
downloader = http.MediaIoBaseDownload(outfile, req)
done = False
while done is False:
status, done = downloader.next_chunk()
# print("Download {}%.".format(int(status.progress() * 100)))
print("gsutil cp done")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('bucket', help='storage bucket')
parser.add_argument('filename', help='the file to download')
parser.add_argument('accesstoken', help='access token')
args = parser.parse_args()
main(args.bucket, args.filename, args.accesstoken)
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Cloud Storage authentication - Google Cloud
In the dialogue that appears, click Allow. In Step 2 of the playground, click Exchange authorization code for tokens for the authorization code...
Read more >gsutil cp hangs on obtain initial access_token - Stack Overflow
OK looks like the problem was with the .config files. I deleted the ~/.config/gcloud folder and the issue has resolved itself.
Read more >Top gsutil command lines to get started on Google Cloud ...
When you use a service account to authenticate your application, you do not need a user to authenticate to get an access token....
Read more >gsutil: Command-Line Control of Google Cloud Storage
1. Using your machine terminal, issue the command gsutil help mb (Fig. 2.). This help command will provide you all the syntax details ......
Read more >Create access credentials | Google Workspace
In the Google Cloud console, go to Menu menu > APIs & Services > Credentials. · Click Create Credentials > OAuth client ID....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I also am trying to use gsutil without Service Account key. (My employer prohibits use of SA keys. Ironically, my employer is Google.) Documenting what worked.
There are 3 versions of
gsutil
:gsutil
. This is the oldest, and predatesgcloud
.gsutil
as part of gcloudgcloud alpha storage
. This is the newest. Faster than 2.For 2: Write the
~/.config/gcloud/legacy_credentials/default/.boto
thatgoogle-cloud-sdk/bin/bootstrapping/gsutil.py
expects:For 3: Do what rest of
gcloud does
: setCLOUDSDK_AUTH_ACCESS_TOKEN
. Note that you’ll need Python 3 installed. If you don’t have Python 3, you’ll getTypeError: 'instance has no next() method'
.I used mitmproxy to troubleshoot the request and found out I was just missing the
alt=media
query param. I’m closing this and will just usecurl
for docker builds.