20

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)
mazaneicha
  • 8,794
  • 4
  • 33
  • 52
joey
  • 345
  • 1
  • 2
  • 9
  • The error seems to imply your bucket name contains a whitespace at the end of it.. which the regex does not allow. – FatalError Jan 05 '19 at 05:09
  • 2
    Your regex actually does not allow a hyphen `-` because hyphen acts as a range specifier in your character class. If you want to literally include a hyphen then change your regex to this `^[a-zA-Z0-9.-_-]{1,255}$` – Pushpesh Kumar Rajwanshi Jan 05 '19 at 05:10
  • 3
    @PushpeshKumarRajwanshi the regex is part of the error message, coming from either the SDK or the service itself. Markdown was suppessing a backslash. Edited. – Michael - sqlbot Jan 05 '19 at 14:43
  • 3
    Your image_key should be quoted. – Toto Jan 05 '19 at 14:43
  • The regex does allow for a hyphen and is escaped as such. [a-zA-Z0-9.\-_]. The issue is the trailing whitespace as @FatalError found. – UtahJarhead Jan 05 '19 at 14:44
  • @Michael-sqlbot is correct. It's coming back from Amazon's `boto3` library. – UtahJarhead Jan 05 '19 at 14:45

6 Answers6

17

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
3

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.

Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
  • Upvoted. UTF8 with BOM is working for me but this answer led me to finding that I had CRLF in my sh file with the bucket name which was causing the problem for me. – Ben Allred Jun 14 '22 at 21:04
1

I had the same issue and it had a mistake while I stored some path in a consts file

PATH = 'gs://same_bucket/same_folder/some_file.csv'

and in other script tried to read it with additional gs://

df = spark.read.csv(f"gs://{PATH})

0

If you are running the code in Jupyter code then make sure you don't place your bucket names as str like "bucket_name" it should be just bucket_name=name

srilalitha
  • 19
  • 1
0

If you would like to create sub-folders inside the bucket, you can prefix the locations in this File_key variable. For example, /subfolder/file_name.txt

ben othman zied
  • 176
  • 1
  • 7
0

Check if have erased the <> characters already and just write the bucket name between the quotes ''