Storage/S3: Truncate bucket hash if necessary

This commit is contained in:
Adrian Moennich 2018-11-26 14:50:14 +01:00
parent 71a679267c
commit 3a32e524f8
2 changed files with 5 additions and 3 deletions

View File

@ -223,7 +223,7 @@ class DynamicS3Storage(S3StorageBase):
def _get_bucket_name(self, date):
name = self._replace_bucket_placeholders(self.bucket_name_template, date)
token = hmac.new(self.bucket_secret.encode('utf-8'), name, hashlib.md5).hexdigest()
return '{}-{}'.format(name, token)
return '{}-{}'.format(name, token)[:63]
def _replace_bucket_placeholders(self, name, date):
name = name.replace('<year>', date.strftime('%Y'))

View File

@ -102,8 +102,10 @@ def test_static_bucket_name_too_long():
def test_dynamic_bucket_name_too_long():
DynamicS3Storage('bucket_secret=secret,bucket_template=test-<year>' + 'x'*37)
DynamicS3Storage('bucket_secret=secret,bucket_template=test-<year>-<month>' + 'x'*34)
s = DynamicS3Storage('bucket_secret=secret,bucket_template=test-<year>' + 'x'*37)
assert len(s._get_current_bucket_name()) == 63
s = DynamicS3Storage('bucket_secret=secret,bucket_template=test-<year>-<month>' + 'x'*34)
assert len(s._get_current_bucket_name()) == 63
with pytest.raises(StorageError):
DynamicS3Storage('bucket_secret=secret,bucket_template=test-<year>' + 'x' * 38)
with pytest.raises(StorageError):