Przeglądaj źródła

feat: 允许阿里云OSS通过ACL公共读访问

SongZihuan 2 lat temu
rodzic
commit
b792948252
4 zmienionych plików z 9 dodań i 2 usunięć
  1. 5 1
      aliyun/__init__.py
  2. 2 0
      app/oss.py
  3. 1 0
      configure/__init__.py
  4. 1 1
      templates/index/hello.html

+ 5 - 1
aliyun/__init__.py

@@ -3,6 +3,7 @@ import oss2
 import logging.handlers
 import logging
 import os
+from urllib.parse import urljoin
 
 
 class Aliyun:
@@ -29,7 +30,10 @@ class Aliyun:
     def shared_obj(self, name, time=15):
         if not self.bucket.object_exists(name):
             return None
-        url = self.bucket.sign_url('GET', name, time, slash_safe=True)
+        if conf["ALIYUN_BUCKET_USE_SIGN_URL"]:
+            url = self.bucket.sign_url('GET', name, time, slash_safe=True)
+        else:
+            url = urljoin(conf["ALIYUN_BUCKET_ENDPOINT"], name)
         self.logger.debug(f"Get url {url} name: {name} time: {time}s key: {self.key}")
         return url
 

+ 2 - 0
app/oss.py

@@ -50,6 +50,8 @@ def upload_page():
         path: str = form.path.data
         if len(path) > 0 and not path.endswith('/'):
             path += "/"
+        if path.startswith('/'):
+            path = path[1:]
         path += file.filename
         aliyun.upload_file(path, file)
         app.HBlogFlask.print_sys_opt_success_log(f"Upload file {path}")

+ 1 - 0
configure/__init__.py

@@ -28,6 +28,7 @@ conf = {
     "ALIYUN_BUCKET_ENDPOINT": "",
     "ALIYUN_BUCKET_NAME": "",
     "ALIYUN_BUCKET_IS_CNAME": False,
+    "ALIYUN_BUCKET_USE_SIGN_URL": True,
     "LOG_HOME": "",
     "LOG_FORMAT": "[%(levelname)s]:%(name)s:%(asctime)s "
                   "(%(filename)s:%(lineno)d %(funcName)s) "

+ 1 - 1
templates/index/hello.html

@@ -7,7 +7,7 @@
     <link href="{{ url_for('static', filename='styles/index/hello.css') }}" rel="stylesheet">
     <style>
         body {
-            background-image: url("{{ url_for('oss.get_page', name="/hblog/bg-img.jpg") }}");
+            background-image: url("{{ url_for('oss.get_page', name="hblog/bg-img.jpg") }}");
             overflow-y: hidden;
             overflow-x: hidden;
         }