【问题标题】:Python not recognizing my edited BOTO filePython 无法识别我编辑的 BOTO 文件
【发布时间】:2016-08-22 18:52:44
【问题描述】:

我正在按照此链接中的说明通过 Python 创建 Google Cloud 存储桶。 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 文件中的凭据。

【问题讨论】:

  • 作为第一步,您能否确保您希望使用的凭据文件实际上在您的路径上被 boto 识别?我建议这样的代码: print boto.pyami.config.BotoConfigLocations 然后,假设可行,确保加载的配置符合您的期望: print boto.pyami.config.Config().dump()
  • 太棒了。那是一个很好的答案。它告诉我它正在寻找 ['/etc/boto.cfg', '~/.boto'] 中的配置文件。但是,当我要求它转储时。它说没有。此外,当我输入 boto.pyami.config 时,它会显示“NameError:'both' 未定义,未定义来自导入的变量:pyami”。我该怎么办这个错误?我没有得到任何转储。
  • 如果它说 'both' 没有定义,听起来就像一个错字?你是说'boto'吗?

标签: python-2.7 google-cloud-storage boto


【解决方案1】:

好的,所以 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 配置文件的情况下完成并做任何你想做的事情。

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多