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

时间:2019-01-05 05:07:46

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

当我尝试将图像上传到存储桶时,它会抛出错误"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)

3 个答案:

答案 0 :(得分:2)

"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,其中必须有两个而不是三个反斜杠。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

相关问题