Skip to content

Commit

Permalink
add gitee oss
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Dec 1, 2021
1 parent f2bf25b commit f46fe53
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
6 changes: 5 additions & 1 deletion app/middleware/oss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from app.core.configuration import SystemConfiguration
from app.middleware.oss.aliyun import AliyunOss
from app.middleware.oss.files import OssFile
from app.middleware.oss.gitee import GiteeOss
from config import Config


class OssClient(object):
Expand All @@ -21,7 +23,9 @@ def get_oss_client(cls) -> OssFile:
endpoint = oss_config.get("endpoint")
if oss_config is None:
raise Exception("服务器未配置oss信息, 请在configuration.json中添加")
if oss_config.get("type").lower() == "aliyun":
if oss_config.get("type").lower() == Config.ALIYUN:
return AliyunOss(access_key_id, access_key_secret, endpoint, bucket)
if oss_config.get("type").lower() == Config.GITEE:
return GiteeOss(access_key_secret, bucket, access_key_id)
raise Exception("不支持的oss类型")
return OssClient._client
4 changes: 2 additions & 2 deletions app/middleware/oss/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
class OssFile(ABC):

@abstractmethod
def create_file(self, filepath: str, content: ByteString):
def create_file(self, filepath: str, content: bytes):
pass

@abstractmethod
def update_file(self, filepath: str, content: ByteString):
def update_file(self, filepath: str, content: bytes):
pass

@abstractmethod
Expand Down
37 changes: 37 additions & 0 deletions app/middleware/oss/gitee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import base64

import requests

from app.middleware.oss import OssFile


class GiteeOss(OssFile):
_create_url = "https://gitee.com/api/v5/repos/{}/{}/contents/{}"

def __init__(self, user, repo, token):
self.user = user
self.repo = repo
self.token = token

def create_file(self, filepath: str, content: bytes):
gitee_url = self._create_url.format(self.user, self.repo, filepath)
data = base64.b64encode(content)
json_data = {"access_token": self.token, "message": "pity create file", "content": data}
r = requests.post(url=gitee_url, json=json_data)
if r.status_code != 200:
raise Exception("上传文件到gitee失败")

def update_file(self, filepath: str, content: bytes):
pass

def delete_file(self, filepath: str):
pass

def list_file(self):
pass

def download_file(self, filepath, filename):
pass

def get_file_object(self, filepath):
pass
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Config(object):

SERVER_REPORT = "http://test.pity.fun/#/record/report/"

ALIYUN = "aliyun"
GITEE = "gitee"

# 请求类型
class BodyType:
none = 0
Expand Down
8 changes: 4 additions & 4 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"token": ""
},
"oss": {
"type": "aliyun",
"access_key_id": "",
"access_key_secret": "",
"bucket": "",
"type": "gitee",
"access_key_id": "56a5797a2fb4a3cdfca439082da7dee1",
"access_key_secret": "woodywrx",
"bucket": "oss",
"endpoint": ""
}
}

0 comments on commit f46fe53

Please sign in to comment.