Python “地位”:500,”;正文“:&引用;HTTP错误400:无效的URI:isHexDigit";使用skimage将s3上的图像下载到本地时
我的任务是每次使用随机URL将文件上传到s3 bucket(public)。为此,我使用以下代码生成URLPython “地位”:500,”;正文“:&引用;HTTP错误400:无效的URI:isHexDigit";使用skimage将s3上的图像下载到本地时,python,amazon-web-services,url,amazon-s3,boto3,Python,Amazon Web Services,Url,Amazon S3,Boto3,我的任务是每次使用随机URL将文件上传到s3 bucket(public)。为此,我使用以下代码生成URL 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
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时,它显示HTTP 400。当我尝试使用Skipage下载相同的内容时,它显示
状态:500,“正文”:“HTTP错误400:无效URI:isHexDigit”
这里:扩展:image\u扩展,今天:日期对象,BUCKET:s3 BUCKET,缓冲区:image对象,区域:s3 BUCKET位置
下面附加了一个示例url:您生成的url无效。保留了%字符 下面是一个简单的示例,它将重现您的错误;
imread()
在内部调用urllib.request.urlopen()
,异常源于该调用:
导入urllib
地区='us-east-1'
BUCKET=‘BUCKET’
image_path='FOLDER/932724r8477P9577%4-4-2020.jpeg'
对象url=”https://s3.{0}.amazonaws.com/{1}/{2}”。格式(
区域
水桶
图像(U路径)
f=urllib.request.urlopen(对象\ url)
上述操作将因以下原因而失败:
urllib.error.HTTPError:HTTP错误400:无效URI:isHexDigit
您需要像这样进行百分比编码:
导入urllib
地区='us-east-1'
BUCKET=‘BUCKET’
image_path='FOLDER/932724r8477P9577%4-4-2020.jpeg'
对象url=”https://s3.{0}.amazonaws.com/{1}/{2}”。格式(
区域
水桶
urllib.parse.quote(图像路径))
f=urllib.request.urlopen(对象\ url)
生成的URL将正确编码:
https://s3.us-east-1.amazonaws.com/BUCKET/FOLDER/932724r8477P9577%254-4-2020.jpeg