【问题标题】:ERROR: Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"错误:存储桶名称必须匹配正则表达式“^[a-zA-Z0-9.\-_]{1,255}$”
【发布时间】:2019-05-31 15:04:53
【问题描述】:

当我尝试将图像上传到存储桶时,它会抛出错误 "Invalid bucket name "thum.images ": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$""

我认为存储桶名称没有什么问题。

这是我上传图片的代码:

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)

【问题讨论】:

  • 该错误似乎暗示您的存储桶名称在其末尾包含一个空格.. 正则表达式不允许。
  • 您的正则表达式实际上不允许使用连字符-,因为连字符在您的字符类中充当范围说明符。如果您想从字面上包含连字符,请将您的正则表达式更改为此 ^[a-zA-Z0-9.-_-]{1,255}$
  • @PushpeshKumarRajwanshi 正则表达式是错误消息的一部分,来自 SDK 或服务本身。 Markdown 正在取消反斜杠。已编辑。
  • 你的 image_key 应该被引用。
  • 正则表达式确实允许使用连字符并被转义。 [a-zA-Z0-9.\-_]。问题是@FatalError 发现的尾随空格。

标签: python regex amazon-web-services amazon-s3 bucket


【解决方案1】:

"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 字母、数字、点、-_ 字符
  • $ - 字符串结束。

您可以test your bucket names online here

存储桶名称中不能有空格。

我经常收到此错误,因为在我从 S3 网页复制/粘贴存储桶名称后,存储桶名称中出现了一个额外的斜杠,例如 aws s3 sync s3:///my-bucket/folder folder,其中必须只有两个反斜杠,而不是三个反斜杠。

【讨论】:

  • 根据那个正则表达式,正斜杠不是无效字符吗?
【解决方案2】:

我收到此错误是因为我在包含 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 的输出(即两位十六进制数字)。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-10-22
      • 2015-01-07
      • 2021-11-09
      • 2010-12-11
      • 2015-02-11
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多