状态”:500,“ body”:“ HTTP错误400:无效的URI:isHexDigit”,同时使用skimage将s3上的图像下载到本地时

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

我的任务是每次使用随机网址将文件上传到s3存储桶(公共)。为此,我使用以下代码生成网址

image_path = FOLDER+'/'+str(random.randint(0, 1000000))  + str(random.randint(0, 1000000))  + str(random.randint(0, 1000000)) + '%' + str(today.day) + '-' + str(today.month) + '-' + str(today.year)+'.'+extension

s3.put_object(
        Bucket=BUCKET,
        Key = image_path,
        Body = buffer,
        ContentType = 'image/'+extension,
        ACL = 'public-read'

    )
object_url = "https://s3-{0}.amazonaws.com/{1}/{2}".format(
        REGION,
        BUCKET,
        image_path)
 from skimage import io
 image = io.imread(object_url)

上传没有显示错误。虽然现在当我尝试使用浏览器访问该URL时,它显示为HTTP400。当我尝试使用skimage下载该URL时,它显示了状态]:500,“正文”:“ HTTP错误400:无效的URI:isHexDigit”此处:extension:image_extension,today:日期对象,BUCKET:s3存储桶,buffer:图像对象,REGION:s3存储桶位置网址示例如下:https://s3-REGION.amazonaws.com/BUCKET/FOLDER/932724r8477P9577%4-4-2020.jpeg

python amazon-web-services url amazon-s3 boto3
1个回答
0
投票

您生成的URL无效。百分比字符保留用于percent encoding

一个精简的示例将重现您的错误:

import urllib

REGION = 'us-east-1'
BUCKET = 'BUCKET'
image_path = 'FOLDER/932724r8477P9577%4-4-2020.jpeg'

object_url = "https://s3.{0}.amazonaws.com/{1}/{2}".format(
        REGION,
        BUCKET,
        image_path)

f = urllib.request.urlopen(object_url)

以上将失败:

urllib.error.HTTPError: HTTP Error 400: Invalid URI: isHexDigit

您需要像这样进行百分比编码:

import urllib

REGION = 'us-east-1'
BUCKET = 'BUCKET'
image_path = 'FOLDER/932724r8477P9577%4-4-2020.jpeg'

object_url = "https://s3.{0}.amazonaws.com/{1}/{2}".format(
        REGION,
        BUCKET,
        urllib.parse.quote(image_path))

f = urllib.request.urlopen(object_url)
© www.soinside.com 2019 - 2024. All rights reserved.