Nikolas Daroit
Nikolas Daroit

Reputation: 73

'foo.bar.com.s3.amazonaws.com' doesn't match either of '*.s3.amazonaws.com', 's3.amazonaws.com'

I'm using django, and things like imgs I store at s3 (for this I'm using boto), but recently I got this error:

'foo.bar.com.s3.amazonaws.com' doesn't match either of '*.s3.amazonaws.com', 's3.amazonaws.com'

I'm searching for a possible solution for about two days, but the unique things that is suggested is to change boto's source code, however I can't do this on production.

Edit: Using Django 1.58, Boto 2.38.0

Any help would be appreciated. Thx in advance.

Upvotes: 4

Views: 4757

Answers (4)

Eyal Ch
Eyal Ch

Reputation: 10066

You can use this monkey patch in your connection.py file (boto/connection.py):

import ssl

_old_match_hostname = ssl.match_hostname

def _new_match_hostname(cert, hostname):
   if hostname.endswith('.s3.amazonaws.com'):
      pos = hostname.find('.s3.amazonaws.com')
      hostname = hostname[:pos].replace('.', '') + hostname[pos:]
   return _old_match_hostname(cert, hostname)

ssl.match_hostname = _new_match_hostname

(source)

another solution is in here:

Upvotes: 4

diemacht
diemacht

Reputation: 2102

I had the same error when I moved from python3.4 to python3.5 (boto version remained the same).

The problem was solved by moving from boto to boto3 package.

Example:

import boto3
s3_client = boto3.client("s3", aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
body = open(local_filename, 'rb').read()
resp = s3_client.put_object(ACL="private", Body=body, Bucket=your_bucket_name, Key=path_to_the_file_inside_the_bucket)
if resp["ResponseMetadata"]["HTTPStatusCode"] != 200:
        raise Exception("Something went wrong...")

Upvotes: 1

Eduardo
Eduardo

Reputation: 1837

As has been said, the problem occurs in buckets containing dots on name. Below just an example that prevents this.

import boto
from boto.s3.connection import VHostCallingFormat

c = boto.connect_s3(aws_access_key_id='your-access-key',
                    is_secure=False,
                    aws_secret_access_key='your-secret-access',
                    calling_format=VHostCallingFormat())
b = c.get_bucket(bucket_name='your.bucket.with.dots', validate=True)
print(b)

Upvotes: 7

Louis Barranqueiro
Louis Barranqueiro

Reputation: 10238

It's an known issue : #2836. It's due to dots in your bucket name. I had this issue some days before. A user seems to have succeeded to fix this by setting :

AWS_S3_HOST = 's3-eu-central-1.amazonaws.com'
AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat

But it didn't work for me.

Otherwise, you can create a bucket without points (E.g : foo-bar-com). It will work. This is what I did to temporarily fix this issue.

Upvotes: 2

Related Questions