File size: 1,340 Bytes
5c78c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import uuid
from io import BytesIO
from PIL import Image
import base64


def uploadBase64(bucketName,endpoint,imgBase64, filename):
    
    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图像的字节数据
    jpg_image_data = jpg_image_stream.getvalue()


    auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
    # 填写Bucket名称。
    bucket = oss2.Bucket(auth, f'https://{endpoint}', bucketName)
    result = bucket.put_object(filename, jpg_image_data)
    # 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']))