Skip to content

Commit

Permalink
feat: s3 sdk
Browse files Browse the repository at this point in the history
delete cos sdk for #767
  • Loading branch information
SAKURA-CAT committed Jan 20, 2025
1 parent f6101e3 commit c05a2e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
swankit==0.1.2b6
swanboard==0.1.7b1
cos-python-sdk-v5
urllib3>=1.26.0
requests>=2.25.0
setuptools
click
pyyaml
psutil>=5.0.0
pynvml
boto3>=1.35.49
botocore
35 changes: 15 additions & 20 deletions swanlab/api/cos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
@Description:
tencent cos
"""
# noinspection PyPackageRequirements
from qcloud_cos import CosConfig
# noinspection PyPackageRequirements
from qcloud_cos import CosS3Client
# noinspection PyPackageRequirements
from qcloud_cos.cos_threadpool import SimpleThreadPool
from datetime import datetime, timedelta
from typing import List, Dict, Union
from typing import List

import boto3
from botocore.config import Config as BotocoreConfig

from swanlab.data.modules import MediaBuffer
from swanlab.log import swanlog

Expand All @@ -30,14 +28,15 @@ def __init__(self, data):
self.__prefix = data["prefix"]
self.__bucket = data["bucket"]
credentials = data["credentials"]
config = CosConfig(
Region=data["region"],
SecretId=credentials['tmpSecretId'],
SecretKey=credentials['tmpSecretKey'],
Token=credentials['sessionToken'],
Scheme='https'
self.__client = boto3.client(
's3',
endpoint_url=f"https://cos.{data['region']}.myqcloud.com",
api_version='2006-03-01',
aws_access_key_id=credentials['tmpSecretId'],
aws_secret_access_key=credentials['tmpSecretKey'],
aws_session_token=credentials['sessionToken'],
config=BotocoreConfig(signature_version="s3", s3={'addressing_style': 'virtual'}),
)
self.__client = CosS3Client(config)

def upload(self, buffer: MediaBuffer):
"""
Expand All @@ -52,24 +51,20 @@ def upload(self, buffer: MediaBuffer):
Bucket=self.__bucket,
Key=key,
Body=buffer.getvalue(),
EnableMD5=False,
# 一年
CacheControl="max-age=31536000",
)
except Exception as e:
swanlog.error("Upload error: {}".format(e))

def upload_files(self, buffers: List[MediaBuffer]) -> Dict[str, Union[bool, List]]:
def upload_files(self, buffers: List[MediaBuffer]):
"""
批量上传文件,keys和local_paths的长度应该相等
:param buffers: 本地文件的二进制对象集合
"""
pool = SimpleThreadPool()
# TODO 线程池
for buffer in buffers:
self.upload(buffer)
pool.wait_completion()
result = pool.get_result()
return result

@property
def should_refresh(self):
Expand Down

0 comments on commit c05a2e0

Please sign in to comment.