您好,登錄后才能下訂單哦!
這篇文章主要講解了“boto支持aws4引發的錯誤怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“boto支持aws4引發的錯誤怎么解決”吧!
軟件版本
root@demo:/home/demouser# ceph -v ceph version 10.2.6 (656b5b63ed7c43bd014bcafd81b001959d5f089f) boto版本:2.46.1
rgw配置
[client.radosgw.cn-zone1] rgw dns name = ceph.work rgw frontends = fastcgi socket_port=9000 socket_host=127.0.0.1 host = demo keyring = /etc/ceph/ceph.client.radosgw.keyring rgw socket path = /home/ceph/var/run/ceph-client.radosgw.cn-zone1.sock log file = /home/ceph/log/radosgw.cn-zone1.log rgw print continue = false rgw content length compat = true
boto用例
from boto.s3.connection import S3Connection import boto import os os.environ['S3_USE_SIGV4'] = 'True' #啟動對aws4的支持 endpoint = 'ceph.work' bucket_name = 'test1' access_key = '' secret_key = '' conn = boto.connect_s3( aws_access_key_id=access_key, aws_secret_access_key=secret_key, host=endpoint, is_secure=False, calling_format=boto.s3.connection.SubdomainCallingFormat(), validate_certs=True, ) bucket = conn.get_all_buckets() print bucket
異常信息
Traceback (most recent call last): ..... File "/Users/demouser/lwc/lib/python2.7/site-packages/boto/auth.py", line 690, in determine_region_name return region_name UnboundLocalError: local variable 'region_name' referenced before assignment
boto對region_name的邏輯處理
def split_host_parts(self, host): return host.split('.') def determine_region_name(self, host): # S3's different format(s) of representing region/service from the # rest of AWS makes this hurt too. # # Possible domain formats: # - s3.amazonaws.com (Classic) # - s3-us-west-2.amazonaws.com (Specific region) # - bukkit.s3.amazonaws.com (Vhosted Classic) # - bukkit.s3-ap-northeast-1.amazonaws.com (Vhosted specific region) # - s3.cn-north-1.amazonaws.com.cn - (Beijing region) # - bukkit.s3.cn-north-1.amazonaws.com.cn - (Vhosted Beijing region) parts = self.split_host_parts(host) if self.region_name is not None: region_name = self.region_name else: # Classic URLs - s3-us-west-2.amazonaws.com if len(parts) == 3: region_name = self.clean_region_name(parts[0]) # Special-case for Classic. if region_name == 's3': region_name = 'us-east-1' #這里有個坑,下面會講 else: # Iterate over the parts in reverse order. for offset, part in enumerate(reversed(parts)): part = part.lower() # Look for the first thing starting with 's3'. # Until there's a ``.s3`` TLD, we should be OK. :P if part == 's3': # If it's by itself, the region is the previous part. region_name = parts[-offset] # Unless it's Vhosted classic if region_name == 'amazonaws': region_name = 'us-east-1' break elif part.startswith('s3-'): region_name = self.clean_region_name(part) break return region_name
走讀boto代碼,發現其實region_name字段就是將host按"."進行切分,取前面部分進行處理得出,如果host為ceph.work,那么切分出來的"ceph"當然對應不上了,所以想要讓boto支持AWS4,你的host必須有3個字段,比如"s3.ceph.work"。如果你采取掩耳盜鈴方式,只是去調整boto代碼如下
from boto.s3.connection import S3Connection import boto import os os.environ['S3_USE_SIGV4'] = 'True' endpoint = 's3.ceph.work' #新增一個字段 bucket_name = 'test1' access_key = '' secret_key = '' conn = boto.connect_s3( aws_access_key_id=access_key, aws_secret_access_key=secret_key, host=endpoint, is_secure=False, calling_format=boto.s3.connection.SubdomainCallingFormat(), validate_certs=True, ) bucket = conn.get_all_buckets() print bucket
那么你將看到如下異常
Traceback (most recent call last): ... File "/Users/Diluga/lwc/lib/python2.7/site-packages/boto/s3/connection.py", line 444, in get_all_buckets response.status, response.reason, body) boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request <?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidBucketName</Code><BucketName>s3</BucketName><RequestId>tx000000000000000000001-0058d4d9ad-85cc-default</RequestId><HostId>85cc-default-default</HostId></Error>
這個400錯誤,說明你提交數據的姿勢不對,
從抓包來看這個us-east-1就是你提交的region_name了,這里也給我們埋了一個坑,如果我們的host第一個字段是"s3",那么region_name就是被硬編碼成了"us-east-1",如果是"s3-abc"或者是其他,則region_name變成我們自定義的字段,好在ceph默認是允許你在request header里面隨便填region_name,不然就真的悲劇了。
這里正確的姿勢應該是調整ceph.conf里面的rgw配置如下.
方案1
[client.radosgw.cn-zone1] rgw dns name = s3.ceph.work #原有host基礎上新增一個字段,boto提交的region_name會是"us-east-1" rgw frontends = fastcgi socket_port=9000 socket_host=127.0.0.1 host = demo keyring = /etc/ceph/ceph.client.radosgw.keyring rgw socket path = /home/ceph/var/run/ceph-client.radosgw.cn-zone1.sock log file = /home/ceph/log/radosgw.cn-zone1.log rgw print continue = false rgw content length compat = true
方案2
[client.radosgw.cn-zone1] rgw dns name = s3-abc.ceph.work #原有host基礎上新增一個字段,boto提交的region_name會是"abc" rgw frontends = fastcgi socket_port=9000 socket_host=127.0.0.1 host = demo keyring = /etc/ceph/ceph.client.radosgw.keyring rgw socket path = /home/ceph/var/run/ceph-client.radosgw.cn-zone1.sock log file = /home/ceph/log/radosgw.cn-zone1.log rgw print continue = false rgw content length compat = true
感謝各位的閱讀,以上就是“boto支持aws4引發的錯誤怎么解決”的內容了,經過本文的學習后,相信大家對boto支持aws4引發的錯誤怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。