Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"

When I try to upload images to a bucket, it throw an error "Invalid bucket name "thum.images ": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"".

I think there is nothing wrong with a bucket name.

This is my code to upload image:

def upload_thumbnail_image(image_key, thumbnail_image):
    thumbnail_image_bucket = os.environ['thumbnail_bucket']
    thumbnail_image = #image path
    image_key = EFE3-27C8-EEB3-4987/3612d0bc-bdfd-49de-82ee-3e66cbb06807.jpg
    try:
        new_object = client.upload_file(thumbnail_image, thumbnail_image_bucket, image_key)
        return new_object
    except Exception as Exc:
        set_log(Exc.args[0],True)
like image 979
joey Avatar asked Jan 05 '19 05:01

joey


2 Answers

The "Invalid bucket name "thum.images ": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"" error means just what it says: the bucket name must contain some typo or is just wrong as it should meet the following pattern:

  • ^ - start of string
  • [a-zA-Z0-9.\-_]{1,255} - 1 to 255 ASCII letters, digits, dots, - or _ chars
  • $ - end of string.

You may test your bucket names online here.

There can be no whitespaces in the bucket name.

I often get this error because an extra slash gets into the bucket name after I copy/paste the bucket name from the S3 Web page, like aws s3 sync s3:///my-bucket/folder folder, where instead of the triple backslashes there must be just two.

like image 179
Wiktor Stribiżew Avatar answered Nov 18 '22 04:11

Wiktor Stribiżew


I received this error because I had an invisible non-printing character (the BOM, aka Byte Order Mark, aka U+FEFF) at the beginning of an csv file that contained the s3 path. I was able to find it with this python code:

print(":".join("{:02x}".format(ord(c)) for c in s3_path))

which resulted in feff:... at the beginning of the string which tipped me off. You would expect to see output like 6d:79:2d:70:61:74:68 (i.e. two digit hex numbers).

(Update 2022) As per the comment from Ben Allred, there are other non-printing characters that could also cause the same error and be difficult to detect.

like image 3
Mark Chackerian Avatar answered Nov 18 '22 04:11

Mark Chackerian