【问题标题】:How to read urls using "With open()"? [duplicate]如何使用“With open()”读取网址? [复制]
【发布时间】:2020-09-13 17:44:10
【问题描述】:

我正在这样做并且工作得很好:

def b64_image(filename):
    with open("static/img/"+filename, 'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode('utf-8')

但现在我将图像上传到 S3 存储桶,所以我编辑了代码:

def b64_image(filename):
    with open("https://mybucket.s3-sa-east-1.amazonaws.com/" + filename, 'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode('utf-8')

我收到以下错误:

FileNotFoundError: [Errno 2] No such file or directory:

我怎样才能使这段代码工作?如何使代码查找实际的 URL 图像?提前致谢。

【问题讨论】:

  • 您需要使用 boto3 来访问 S3 上的文件。你看过吗?

标签: python image amazon-web-services file


【解决方案1】:

为了在S3 中的文件上使用open,您需要使用第3 方库。特别是,这个维护和活跃的库 smart-open 就是这样做的。

【讨论】:

  • 我没用过,但绝对是一个选择,谢谢!
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 2012-12-06
  • 1970-01-01
  • 2018-09-15
  • 2013-01-07
  • 2022-07-19
相关资源
最近更新 更多