我按照此链接中的说明操作,以便通过Python创建Google云端存储分区。 https://cloud.google.com/storage/docs/xml-api/gspythonlibrary
我已按照所有说明操作,并使用我的所有凭据创建了一个启动文件。我打开了这两个文件,我可以看到我的gs_access_key_id和gs_secret_access_key在那里,文件已保存。
import webapp2
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/2.7/lib/py thon2.7/site-packages")
import boto
import gcs_oauth2_boto_plugin
import os
import shutil
import StringIO
import tempfile
import time
GOOGLE_STORAGE = 'gs'
LOCAL_FILE = 'file'
CLIENT_ID = ''
CLIENT_SECRET = ''
gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CLIENT_ID, CLIENT_SECRET)
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world! This should work ! I have been working no this shit all day!')
now = time.time()
CATS_BUCKETS = 'cats-%d' % now
DOGS_BUCKETS = 'cats-%d' % now
project_id = 'name-of-my-bucket'
for name in (CATS_BUCKETS, DOGS_BUCKETS):
uri = boto.storage_uri(name, GOOGLE_STORAGE)
try:
header_values={'x-google-project-id': project_id}
uri.create_bucket()
print 'Successfully created bucket "%s"' %name
except boto.exception.StorageCreateError, e:
print 'Failed to create bucket:', e
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
但是,在尝试create_bucket的行中,我收到错误。我调试了它,它回来说从未找到gs_access_key_id,但它显然在我的.boto文件中。
这是我尝试在localhost中运行此程序时遇到的错误。
File "/Users/LejendVega/Desktop/create-buckets-adrian/main.py", line 48, in get
uri.create_bucket()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/storage_uri.py", line 558, in create_bucket
conn = self.connect()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/storage_uri.py", line 140, in connect
**connection_args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/gs/connection.py", line 47, in __init__
suppress_consec_slashes=suppress_consec_slashes)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/s3/connection.py", line 191, in __init__
validate_certs=validate_certs, profile_name=profile_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/connection.py", line 569, in __init__
host, config, self.provider, self._required_auth_capability())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/boto/auth.py", line 989, in get_auth_handler
'Check your credentials' % (len(names), str(names)))
NoAuthHandlerFound: No handler was ready to authenticate. 3 handlers were checked. ['OAuth2Auth', 'OAuth2ServiceAccountAuth', 'HmacAuthV1Handler'] Check your credentials
我想知道的是为什么boto没有识别我的boto文件中的凭据。
答案 0 :(得分:0)
好的,所以boto模块会浏览您的boto配置文件来收集您的凭据,以便在Google Cloud中创建和编辑数据。如果boto模块找不到配置文件,那么您将收到上述错误。我所做的就是,经过3天的尝试弄清楚之后,我只是将我的凭证放在代码中。
from boto import connect_gs
name = 'the-name-of-your-bucket'
gs_conn = connect_gs(gs_access_key_id='YOUR_ACCESS_KEY_ID',
gs_secret_access_key='YOUR_ACCESS_SECRET_KEY')
"""This is the line that creates the actual bucket"""
gs_conn.create_bucket(name)
就这么简单。现在,您可以在没有boto配置文件的情况下完成任何您想要的操作。