Skip to content

Commit

Permalink
支持腾讯云的APPID字段
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Porter committed Jul 11, 2024
1 parent af479b9 commit 8d63eb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 10 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+mysqlconnector://{db_user}:{db_password}@{db_host}/{db_name}"
logger = logging.getLogger(__name__) # Add this line to define the logger object

myS3=S3Utils(os.getenv("S3_SECRET_ID"), os.getenv("S3_SECRET_KEY"), os.getenv("S3_BUCKET"), os.getenv("S3_REGION"))
myS3=S3Utils(os.getenv("S3_SECRET_ID"), os.getenv("S3_SECRET_KEY"), os.getenv("S3_BUCKET"),os.getenv("S3_APPID"), os.getenv("S3_REGION"))



Expand Down Expand Up @@ -371,6 +371,15 @@ def logout():
#200
return flask.jsonify({"code": 200, "message": "success", "data": {}})

@app.route("/s3Config",methods=["get"])
@flask_login.login_required
def s3Config():
data={
"bucket":os.getenv("S3_BUCKET"),
"region":os.getenv("S3_REGION"),
"appid":os.getenv("S3_APPID")
}
return flask.jsonify({"code": 200, "message": "success", "data": data})

#全局修饰器,程序抛出异常会返回500
@app.errorhandler(500)
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
S3_SECRET_ID: "${S3_SECRET_ID}"
S3_SECRET_KEY: "${S3_SECRET_KEY}"
S3_REGION: "${S3_REGION}"
S3_APPID: "${S3_APPID}"
depends_on:
mysql:
condition: service_healthy
Expand Down
14 changes: 8 additions & 6 deletions s3Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import re

class S3Utils:
def __init__(self, secret_id, secret_key, bucket, region):
def __init__(self, secret_id, secret_key, bucket,appid,region):
self.secret_id = secret_id
self.secret_key = secret_key
self.bucket = bucket
self.appid = appid
self.region = region
self.newBucket = bucket + '-' + appid
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)
self.client = CosS3Client(config)

Expand Down Expand Up @@ -68,7 +70,7 @@ def get_credential_demo(self, allow_prefix, role):
'secret_id': self.secret_id,
'secret_key': self.secret_key,

'bucket': self.bucket,
'bucket': self.newBucket,
'region': self.region,
# 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径
# 例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
Expand Down Expand Up @@ -118,29 +120,29 @@ def isValidDir(self, key):

def createDir(self, key):
response = self.client.put_object(
Bucket=self.bucket,
Bucket=self.newBucket,
Body='',
Key=key
)
return response

def uploadObject(self, key, file):
response = self.client.upload_file(
Bucket=self.bucket,
Bucket= self.newBucket,
Key=key,
LocalFilePath=file
)
return response

def getObjectUrl(self, key):
response = self.client.get_object_url(
Bucket=self.bucket,
Bucket= self.newBucket,
Key=key
)
return response
def getPreSignUrl(self, key):
response = self.client.get_presigned_download_url(
Bucket=self.bucket,
Bucket= self.newBucket,
Key=key,
)
return response

0 comments on commit 8d63eb3

Please sign in to comment.