Spaces:
Build error
Build error
| # -*- coding: utf-8 -*- | |
| import oss2 | |
| from oss2.credentials import EnvironmentVariableCredentialsProvider | |
| import uuid | |
| from io import BytesIO | |
| from PIL import Image | |
| import base64 | |
| def base64ToJpg(imgBase64): | |
| png_image_data = base64.b64decode(imgBase64) | |
| png_image = Image.open(BytesIO(png_image_data)) | |
| # 创建一个BytesIO对象用于存储转换后的JPG图像 | |
| jpg_image_stream = BytesIO() | |
| # 将PNG图像转换为JPG图像,并保存到BytesIO对象中 | |
| png_image.convert('RGB').save(jpg_image_stream, format='JPEG') | |
| # 获取JPG图像的字节数据 | |
| return jpg_image_stream.getvalue() | |
| def uploadBase64(bucketName,endpoint,img, filename): | |
| auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider()) | |
| # 填写Bucket名称。 | |
| bucket = oss2.Bucket(auth, f'https://{endpoint}', bucketName, region=endpoint.split('.')[0].split('-', 1)[1]) | |
| result = bucket.put_object(filename, img) | |
| # HTTP返回码。 | |
| print('http status: {0}'.format(result.status)) | |
| # 请求ID。请求ID是本次请求的唯一标识,强烈建议在程序日志中添加此参数。 | |
| print('request_id: {0}'.format(result.request_id)) | |
| # ETag是put_object方法返回值特有的属性,用于标识一个Object的内容。 | |
| print('ETag: {0}'.format(result.etag)) | |
| # HTTP响应头部。 | |
| print('date: {0}'.format(result.headers['date'])) |