简体   繁体   中英

Google Cloud Storage using Python

I set up a required environment for Google Cloud Storage according to the manual. I have installed "gsutil" and set up all paths. My gsutil works perfectly, however, when I try to run the code below,

#!/usr/bin/python

import StringIO
import os
import shutil
import tempfile
import time
from oauth2_plugin import oauth2_plugin

import boto

# URI scheme for Google Cloud Storage.
GOOGLE_STORAGE = 'gs'
# URI scheme for accessing local files.
LOCAL_FILE = 'file'

uri=boto.storage_uri('sangin2', GOOGLE_STORAGE)

try:
    uri.create_bucket()
    print "done!"
except boto.exception.StorageCreateError, e:
    print "failed"

It gives "403 Access denied" error.

  Traceback (most recent call last):
      File "/Volumes/WingIDE-101-4.0.0/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 23, in <module>
      File "/Users/lsangin/gsutil/boto/boto/storage_uri.py", line 349, in create_bucket
        return conn.create_bucket(self.bucket_name, headers, location, policy)
      File "/Users/lsangin/gsutil/boto/boto/gs/connection.py", line 91, in create_bucket
        response.status, response.reason, body)
    boto.exception.GSResponseError: GSResponseError: 403 Forbidden
      <?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message></Error>

Since I am new to this, it is kinda hard for me to figure out why. Can someone help me? Thank you.

The boto library should automatically find and use your $HOME/.boto file. One thing to check: make sure the project you're using is set as your default project for legacy access (at the API console, click on "Storage Access" and verify that it says "This is your default project for legacy access"). When I have that set incorrectly and I follow the create bucket example you referenced, I also get a 403 error, however, it doesn't make sense that this would work for you in gsutil but not with direct use of boto.

Try adding "debug=2" when you instantiate the storage_uri object, like this:

uri = boto.storage_uri(name, GOOGLE_STORAGE, debug=2)

That will generate some additional debugging information on stdout, which you can then compare with the debug output from an analogous, working gsutil example (via gsutil -D mb ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM