错误:存储桶名称必须匹配正则表达式“^[a-zA-Z0-9.\-_]{1,255}$”

问题描述 投票:0回答:6

当我尝试将图像上传到存储桶时,它会抛出错误

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

我觉得bucket的名字没什么问题

这是我上传图片的代码:

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)
python regex amazon-web-services amazon-s3 bucket
6个回答
17
投票

"Invalid bucket name "thum.images ": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$""
错误的意思就是它所说的:存储桶名称必须包含一些拼写错误或者是错误的,因为它应该符合以下模式:

  • ^
    - 字符串的开始
  • [a-zA-Z0-9.\-_]{1,255}
    - 1 到 255 个 ASCII 字母、数字、点、
    -
    _
    字符
  • $
    - 字符串结尾。

您可以在这里在线测试您的存储桶名称.

桶名不能有空格

我经常收到此错误,因为在我从 S3 网页复制/粘贴存储桶名称后,存储桶名称中有一个额外的斜杠,例如

aws s3 sync s3:///my-bucket/folder folder
,其中必须只有两个而不是三重反斜杠。


3
投票

我收到此错误是因为我在包含 s3 路径的 csv 文件的开头有一个不可见的非打印字符(BOM,又名字节顺序标记,又名 U+FEFF)。我能够用这个 python 代码找到它:

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

导致

feff:
... 在字符串的开头,这让我失望了。您会期望看到类似
6d:79:2d:70:61:74:68
(即两位十六进制数)的输出。

(2022 年更新)根据 Ben Allred 的评论,还有其他非打印字符也可能导致相同的错误并且难以检测。


0
投票

如果您在 Jupyter 代码中运行代码,请确保您没有将存储桶名称作为 str ,如“bucket_name”,它应该只是 bucket_name=name


0
投票

如果您想在存储桶中创建子文件夹,可以在此 File_key 变量中为位置添加前缀。例如,/subfolder/file_name.txt


0
投票

检查是否已经删除了 <> 字符,只需在引号之间写上桶名 ''


0
投票

我有同样的问题,当我将此路径存储在常量文件中时出现错误

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

并在其他脚本中尝试使用额外的

gs://

阅读它

df = spark.read.csv(PATH)

© www.soinside.com 2019 - 2024. All rights reserved.