36 lines
993 B
Python
36 lines
993 B
Python
import argparse
|
||
import base64
|
||
import re
|
||
import time
|
||
import alibabacloud_oss_v2 as oss
|
||
import requests
|
||
|
||
from config import region, endpoint, bucket, file_url_pre, bucket_path
|
||
|
||
|
||
def upload_file(file_content: bytes, key: str):
|
||
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
|
||
cfg = oss.config.load_default()
|
||
cfg.credentials_provider = credentials_provider
|
||
cfg.region = region
|
||
cfg.endpoint = endpoint
|
||
client = oss.Client(cfg)
|
||
|
||
result = client.put_object(
|
||
oss.PutObjectRequest(
|
||
bucket=bucket, # 存储空间名称
|
||
key=key, # 对象名称
|
||
body=file_content # 读取文件内容
|
||
)
|
||
)
|
||
# print(result)
|
||
return file_url_pre + key
|
||
|
||
|
||
|
||
|
||
# 脚本入口,当文件被直接运行时调用main函数
|
||
if __name__ == "__main__":
|
||
with open("测试图片数据/IMG_2685.JPG", "rb") as f:
|
||
file_content = f.read()
|
||
upload_file(file_content,key=bucket_path+"test.png") |