wsj1995 commited on
Commit
5c78c2e
·
verified ·
1 Parent(s): f2f406c

Create oss.py

Browse files
Files changed (1) hide show
  1. oss.py +36 -0
oss.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import oss2
3
+ from oss2.credentials import EnvironmentVariableCredentialsProvider
4
+ import uuid
5
+ from io import BytesIO
6
+ from PIL import Image
7
+ import base64
8
+
9
+
10
+ def uploadBase64(bucketName,endpoint,imgBase64, filename):
11
+
12
+ png_image_data = base64.b64decode(imgBase64)
13
+ png_image = Image.open(BytesIO(png_image_data))
14
+
15
+ # 创建一个BytesIO对象用于存储转换后的JPG图像
16
+ jpg_image_stream = BytesIO()
17
+
18
+ # 将PNG图像转换为JPG图像,并保存到BytesIO对象中
19
+ png_image.convert('RGB').save(jpg_image_stream, format='JPEG')
20
+
21
+ # 获取JPG图像的字节数据
22
+ jpg_image_data = jpg_image_stream.getvalue()
23
+
24
+
25
+ auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
26
+ # 填写Bucket名称。
27
+ bucket = oss2.Bucket(auth, f'https://{endpoint}', bucketName)
28
+ result = bucket.put_object(filename, jpg_image_data)
29
+ # HTTP返回码。
30
+ print('http status: {0}'.format(result.status))
31
+ # 请求ID。请求ID是本次请求的唯一标识,强烈建议在程序日志中添加此参数。
32
+ print('request_id: {0}'.format(result.request_id))
33
+ # ETag是put_object方法返回值特有的属性,用于标识一个Object的内容。
34
+ print('ETag: {0}'.format(result.etag))
35
+ # HTTP响应头部。
36
+ print('date: {0}'.format(result.headers['date']))