您好,登錄后才能下訂單哦!
這篇文章主要講解了“boto支持aws4引發(fā)的錯(cuò)誤怎么解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“boto支持aws4引發(fā)的錯(cuò)誤怎么解決”吧!
軟件版本
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' #啟動(dòng)對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' #這里有個(gè)坑,下面會講 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代碼,發(fā)現(xiàn)其實(shí)region_name字段就是將host按"."進(jìn)行切分,取前面部分進(jìn)行處理得出,如果host為ceph.work,那么切分出來的"ceph"當(dāng)然對應(yīng)不上了,所以想要讓boto支持AWS4,你的host必須有3個(gè)字段,比如"s3.ceph.work"。如果你采取掩耳盜鈴方式,只是去調(diào)整boto代碼如下
from boto.s3.connection import S3Connection import boto import os os.environ['S3_USE_SIGV4'] = 'True' endpoint = 's3.ceph.work' #新增一個(gè)字段 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>
這個(gè)400錯(cuò)誤,說明你提交數(shù)據(jù)的姿勢不對,
從抓包來看這個(gè)us-east-1就是你提交的region_name了,這里也給我們埋了一個(gè)坑,如果我們的host第一個(gè)字段是"s3",那么region_name就是被硬編碼成了"us-east-1",如果是"s3-abc"或者是其他,則region_name變成我們自定義的字段,好在ceph默認(rèn)是允許你在request header里面隨便填region_name,不然就真的悲劇了。
這里正確的姿勢應(yīng)該是調(diào)整ceph.conf里面的rgw配置如下.
方案1
[client.radosgw.cn-zone1] rgw dns name = s3.ceph.work #原有host基礎(chǔ)上新增一個(gè)字段,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基礎(chǔ)上新增一個(gè)字段,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引發(fā)的錯(cuò)誤怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對boto支持aws4引發(fā)的錯(cuò)誤怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。