栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Boto3将base64字符串(图像数据)上传到Python中的S3服务器,并获取URL作为回报

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Boto3将base64字符串(图像数据)上传到Python中的S3服务器,并获取URL作为回报

您没有提到如何获得base64。为了重现,我的代码片段使用了

requests
库从互联网上获取图像,然后使用库将其转换为base64
base64

这里的技巧是确保要上传的base64字符串不包含

data:image/jpeg;base64
前缀。而且,正如评论中提到的@dmigo一样,您应该使用
boto3.resource

而不是boto3.client。

    from botocore.vendored import requests    import base64    import boto3    s3 = boto3.resource('s3')    bucket_name = 'BukcetName'    #where the file will be uploaded, if you want to upload the file to folder use 'Folder Name/FileName.jpeg'    file_name_with_extention = 'FileName.jpeg'    url_to_download = 'URL'    #make sure there is no data:image/jpeg;base64 in the string that returns    def get_as_base64(url):        return base64.b64enpre(requests.get(url).content)    def lambda_handler(event, context):        image_base64 = get_as_base64(url_to_download)        obj = s3.Object(bucket_name,file_name_with_extention)        obj.put(Body=base64.b64depre(image_base64))        #get bucket location        location = boto3.client('s3').get_bucket_location(Bucket=bucket_name)['LocationConstraint']        #get object url        object_url = "https://%s.s3-%s.amazonaws.com/%s" % (bucket_name,location, file_name_with_extention)        print(object_url)

有关S3.Object.put的更多信息。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号