首页 > 解决方案 > 获取地址信息错误,socket.gaierror:[Errno -2]

问题描述

我想在 S3 上创建存储桶

import sys
import requests
import requests_aws4auth as aws4auth
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom

access_id = '********'
access_key = '********'
region = 'Frankfurt'

if region and region != 'us-east-1': 
    endpoint = 's3-{}.amazonaws.com'.format(region)
else: 
    endpoint = 's3.amazonaws.com'

auth = aws4auth.AWS4Auth(access_id, access_key, region, 's3')
ns = 'http://s3.amazonaws.com/doc/2006-03-01/'

def xml_pprint(xml_string):
    print(minidom.parseString(xml_string).toprettyxml())

def create_bucket(bucket):
    XML = ET.Element('CreateBucketConfiguration')
    XML.attrib['xmlns'] = ns
    location = ET.SubElement(XML, 'LocationConstraint')
    location.text = auth.region
    data = ET.tostring(XML, encoding='utf-8')
    url = 'http://{}.{}'.format(bucket, endpoint)
    r = requests.put(url, data=data, auth=auth)

    if r.ok:
        print('Created bucket {} OK'.format(bucket))
    else:
        xml_pprint(r.text)

if __name__ == '__main__':
    cmd, *args = sys.argv[1:]
    globals()[cmd](*args)

我懂了

python s3_client.py create_bucket mybucket.example.com
Traceback (most recent call last):
  File "/home/milenko/miniconda3/lib/python3.7/site-packages/urllib3/connection.py", line 171, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/home/milenko/miniconda3/lib/python3.7/site-packages/urllib3/util/connection.py", line 56, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/home/milenko/miniconda3/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

我现在很困惑。我的端点出了点问题。哪个名称或服务不为人知?这是否指向我的 access_id?

标签: amazon-web-servicesamazon-s3

解决方案


需要以不同的方式指定区域

region = 'eu-west-1'

推荐阅读