1. 构建Client类,实现图片读取
1.1 导包&config
client.py
import os
import base64
import numpy as np
import pandas as pd
import cv2
import boto3
# boto3安装: pip3 install opencv-python boto3
config = {
"region_name": "us-east-1",
"endpoint_url": "https://s3.rapidcompute.com",
# "image_bucket": "prod-barwaqt-image",
"aws_access_key_id": "rcus_bd-prod",
"aws_secret_access_key": "OgRKm6h...2HdbKA6s",
}
1.2 类实现
class Client:
def __init__(self):
self.config = config
self.client = boto3.client('s3', **self.config)
def read_image(self, bucket_name, image_key):
try:
response = self.client.get_object(Bucket=bucket_name, Key=image_key)
body = response.get('Body')
tmp = np.frombuffer(body.read(), np.uint8)
image = cv2.imdecode(tmp, cv2.IMREAD_COLOR)
return 'OK', image
except Exception as e:
return 'ERROR', 'READ_IMAGE_ERROR'
def read_image_b64(self, bucket_name, image_key):
status, image = self.read_image(bucket_name, image_key)
if status == 'OK':
retval, buffer = cv2.imencode('.jpg', image)
pic_str = base64.b64encode(buffer)
return status, pic_str.decode()
else:
return status, image</

最低0.47元/天 解锁文章
802

被折叠的 条评论
为什么被折叠?



