mirror of
https://github.com/yv1ing/ezMove.git
synced 2025-09-16 14:51:01 +08:00
new: Completed support for migrating images to Tencent Cloud COS
This commit is contained in:
0
picbed/__init__.py
Normal file
0
picbed/__init__.py
Normal file
37
picbed/cos.py
Normal file
37
picbed/cos.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Tencent COS
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from qcloud_cos import CosConfig
|
||||
from qcloud_cos import CosS3Client
|
||||
from utils import get_current_timestamp
|
||||
import requests
|
||||
import os
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
region = os.getenv('COS_REGION')
|
||||
bucket = os.getenv('COS_BUCKET')
|
||||
secret_id = os.getenv('COS_SECRET_ID')
|
||||
secret_key = os.getenv('COS_SECRET_KEY')
|
||||
save_path = os.getenv('COS_SAVE_PATH')
|
||||
|
||||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=None, Scheme='https')
|
||||
client = CosS3Client(config)
|
||||
|
||||
|
||||
def cos_upload_img(img_old_url):
|
||||
try:
|
||||
stream = requests.get(img_old_url)
|
||||
new_img_name = f'{get_current_timestamp()}.png'
|
||||
resp = client.put_object(
|
||||
Bucket=bucket,
|
||||
Body=stream,
|
||||
Key=new_img_name,
|
||||
)
|
||||
if resp['ETag']:
|
||||
return f'https://{bucket}.cos.{region}.myqcloud.com/{new_img_name}'
|
||||
else:
|
||||
return ''
|
||||
except Exception as e:
|
||||
print(e)
|
||||
Reference in New Issue
Block a user