up2oss 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- up2oss-0.2.0.dist-info/METADATA +90 -0
- up2oss-0.2.0.dist-info/RECORD +6 -0
- up2oss-0.2.0.dist-info/WHEEL +5 -0
- up2oss-0.2.0.dist-info/licenses/LICENSE +29 -0
- up2oss-0.2.0.dist-info/top_level.txt +1 -0
- up2oss.py +324 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: up2oss
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Save uploaded pictures in Aliyun OSS.
|
|
5
|
+
Home-page: https://github.com/sapicd/up2oss
|
|
6
|
+
Author: staugur
|
|
7
|
+
Author-email: staugur@saintic.com
|
|
8
|
+
License: BSD 3-Clause
|
|
9
|
+
Description-Content-Type: text/x-rst
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: oss2>2.0.0
|
|
12
|
+
Dynamic: author
|
|
13
|
+
Dynamic: author-email
|
|
14
|
+
Dynamic: description
|
|
15
|
+
Dynamic: description-content-type
|
|
16
|
+
Dynamic: home-page
|
|
17
|
+
Dynamic: license
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
Dynamic: requires-dist
|
|
20
|
+
Dynamic: summary
|
|
21
|
+
|
|
22
|
+
up2oss
|
|
23
|
+
========
|
|
24
|
+
|
|
25
|
+
这是基于 `sapic <https://github.com/sapicd/sapic>`_ 图床(v2)的一个扩展模块,
|
|
26
|
+
用来将上传的图片保存到阿里云
|
|
27
|
+
`对象存储(OSS) <https://www.aliyun.com/product/oss>`_。
|
|
28
|
+
|
|
29
|
+
- 钩子名(模块名):``up2oss``
|
|
30
|
+
- 扩展类型:``upload``\ (存储驱动)
|
|
31
|
+
- 适用版本:sapic 2.x(V1 的 ``g.cfg`` / ``intpl_localhooksetting`` 契约已废弃)
|
|
32
|
+
|
|
33
|
+
安装
|
|
34
|
+
-----
|
|
35
|
+
|
|
36
|
+
- 正式版本
|
|
37
|
+
|
|
38
|
+
``$ pip install -U up2oss``
|
|
39
|
+
|
|
40
|
+
- 开发版本
|
|
41
|
+
|
|
42
|
+
``$ pip install -U git+https://github.com/sapicd/up2oss.git@master``
|
|
43
|
+
|
|
44
|
+
依赖 ``oss2``;未安装时钩子仍能加载,只是上传会返回
|
|
45
|
+
``Please install oss2 module``。
|
|
46
|
+
|
|
47
|
+
开始使用
|
|
48
|
+
---------
|
|
49
|
+
|
|
50
|
+
添加:
|
|
51
|
+
^^^^^^
|
|
52
|
+
|
|
53
|
+
在 **站点管理-钩子插件** 中导入模块名 ``up2oss``\ (或 ``POST /api/v2/hooks/import``),
|
|
54
|
+
然后启用它。启用后 sapic 会自动创建一条同名存储后端。
|
|
55
|
+
|
|
56
|
+
配置:
|
|
57
|
+
^^^^^^
|
|
58
|
+
|
|
59
|
+
在 **站点管理-存储后端** 里为该后端填写(不是旧的“网站设置”):
|
|
60
|
+
|
|
61
|
+
- ``bucket``\ (必填):OSS 存储空间名称
|
|
62
|
+
- ``access_key_id``\ (必填):阿里云 AccessKey ID,可用有 OSS 管理权限的 RAM 子用户
|
|
63
|
+
- ``secret_key``\ (必填):阿里云 AccessKey Secret,子用户需勾选“编程访问”
|
|
64
|
+
- ``endpoint``:外网 Endpoint,如 ``https://oss-cn-hangzhou.aliyuncs.com``;留空则自动探测(需要列举存储空间权限)
|
|
65
|
+
- ``domain``:公有读用户域名;留空则由 sapic 的 ``/raw/<sha>`` 代理输出
|
|
66
|
+
- ``basedir``:存储根目录,默认桶根目录
|
|
67
|
+
- ``thumb_suffix``:缩略图参数,如 ``?x-oss-process=image/resize,w_400``
|
|
68
|
+
|
|
69
|
+
填好后可点「检查存储空间」验证凭证与桶是否可达。
|
|
70
|
+
|
|
71
|
+
同一个驱动可以建多条存储后端(不同桶、不同密钥、不同优先级),上传走优先级
|
|
72
|
+
最高的那条,其余作为异步副本;判断依据是后端行的 ``priority``\ (数值越小越优先)。
|
|
73
|
+
|
|
74
|
+
私有桶(签名读):
|
|
75
|
+
^^^^^^^^^^^^^^^^^^
|
|
76
|
+
|
|
77
|
+
桶为私有读时,把后端的「访问方式」设为 ``private`` 即可,``domain`` 可留空:
|
|
78
|
+
|
|
79
|
+
- 本驱动实现了 ``upimg_url``,后台不会再报 ``driver cannot read private objects``;
|
|
80
|
+
- 图片直链固定为 ``/raw/<sha>``,sapic 请求本驱动签发 ``Bucket.sign_url`` 的临时地址
|
|
81
|
+
(有效期由核心决定,默认 300 秒),在服务端拉取并流式转发,签名与 AccessKey 都
|
|
82
|
+
不会出现在浏览器;
|
|
83
|
+
- 签名针对 OSS 源站(``{bucket}.{endpoint}``),不针对 CDN/用户域名;
|
|
84
|
+
- ``private`` 后端不再返回 ``src``,需要 CDN 直链的可同时建一个 ``public`` 后端副本。
|
|
85
|
+
|
|
86
|
+
V1 升级提示
|
|
87
|
+
------------
|
|
88
|
+
|
|
89
|
+
V2 的配置存在存储后端行里,V1 站点设置中的 ``aliyun_*`` 不会自动迁移,需要重新
|
|
90
|
+
填写一次;已上传图片的记录与旧直链不受影响。
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
up2oss.py,sha256=CT9NBC6m_HAxeOfuRdIqQubZsmmlwYyUJHeUfCyne14,10996
|
|
2
|
+
up2oss-0.2.0.dist-info/licenses/LICENSE,sha256=_TvIC_P3El-HJSjxjxpYhe_Pi-KHYv3Z2nd8ciPV12U,1519
|
|
3
|
+
up2oss-0.2.0.dist-info/METADATA,sha256=-vW086HsIRfMHYmVu0PuBt-gNjz9kwocyEzadOK6eHs,3318
|
|
4
|
+
up2oss-0.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
up2oss-0.2.0.dist-info/top_level.txt,sha256=Quo3VWl0WodonFdWU3gqWZeLlTkwY79eQZaPeMUAPBA,7
|
|
6
|
+
up2oss-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Hiroshi.tao
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
up2oss
|
up2oss.py
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""up2oss — store objects in Aliyun OSS.
|
|
3
|
+
|
|
4
|
+
Driver contract (v2): ``upimg_save(filename, stream, upload_path, backend=...)``
|
|
5
|
+
returns ``{"code": 0, "path": <oss key>, "size": <bytes>, "etag": <md5>}`` plus
|
|
6
|
+
``src`` when the backend declares a public ``domain``; without it the service
|
|
7
|
+
layer falls back to ``/raw/<sha>``.
|
|
8
|
+
|
|
9
|
+
``upimg_url`` returns a presigned GET address so the same driver can back a
|
|
10
|
+
``private`` bucket: sapic relays the body itself and the signature never reaches
|
|
11
|
+
the browser.
|
|
12
|
+
|
|
13
|
+
Credentials come from ``backend["config"]`` (``bucket`` / ``endpoint`` /
|
|
14
|
+
``access_key_id`` / ``secret_key`` / ``basedir`` / ``domain`` /
|
|
15
|
+
``thumb_suffix``); nothing is read from the site settings any more, so one
|
|
16
|
+
driver can back several buckets with different RAM users.
|
|
17
|
+
|
|
18
|
+
:copyright: (c) 2020 by staugur.
|
|
19
|
+
:license: BSD 3-Clause, see LICENSE for more details.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import logging
|
|
25
|
+
from hashlib import sha256
|
|
26
|
+
|
|
27
|
+
try: # pragma: no cover - optional dependency
|
|
28
|
+
from oss2 import Auth, Bucket, BucketIterator, Service
|
|
29
|
+
except ImportError: # pragma: no cover - optional dependency
|
|
30
|
+
Auth = Bucket = BucketIterator = Service = None
|
|
31
|
+
|
|
32
|
+
__version__ = '0.2.0'
|
|
33
|
+
__author__ = 'staugur <staugur@saintic.com>'
|
|
34
|
+
__description__ = '将图片保存到阿里云'
|
|
35
|
+
__catalog__ = 'upload'
|
|
36
|
+
__state__ = 'disabled'
|
|
37
|
+
__appversion__ = '2.x'
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger(__name__)
|
|
40
|
+
|
|
41
|
+
FIELDS = (
|
|
42
|
+
(
|
|
43
|
+
'domain',
|
|
44
|
+
'input',
|
|
45
|
+
'用户域名',
|
|
46
|
+
'OSS 绑定的公有读用户域名;私有桶留空,由 sapic 的 /raw 转发读取',
|
|
47
|
+
),
|
|
48
|
+
(
|
|
49
|
+
'thumb_suffix',
|
|
50
|
+
'input',
|
|
51
|
+
'缩略图参数',
|
|
52
|
+
'图片处理后缀,如 ?x-oss-process=image/resize,w_400;留空则列表页加载原图',
|
|
53
|
+
),
|
|
54
|
+
('bucket', 'input', 'Bucket', 'OSS 存储空间名称'),
|
|
55
|
+
(
|
|
56
|
+
'endpoint',
|
|
57
|
+
'input',
|
|
58
|
+
'Endpoint',
|
|
59
|
+
'存储空间所在地域的外网 Endpoint,如 https://oss-cn-hangzhou.aliyuncs.com;留空则自动探测',
|
|
60
|
+
),
|
|
61
|
+
(
|
|
62
|
+
'access_key_id',
|
|
63
|
+
'password',
|
|
64
|
+
'AccessKeyID',
|
|
65
|
+
'阿里云 AccessKey 的 ID,支持有 OSS 管理权限的 RAM 子用户',
|
|
66
|
+
),
|
|
67
|
+
(
|
|
68
|
+
'secret_key',
|
|
69
|
+
'password',
|
|
70
|
+
'SecretKey',
|
|
71
|
+
'阿里云 AccessKey 的 Secret,RAM 子用户需勾选编程访问',
|
|
72
|
+
),
|
|
73
|
+
('basedir', 'input', '存储根目录', '图片存储到 OSS 的基础目录,默认是根目录'),
|
|
74
|
+
)
|
|
75
|
+
REQUIRED = ('bucket', 'access_key_id', 'secret_key')
|
|
76
|
+
#: Service endpoint used to look up a bucket's endpoint when it is not filled in.
|
|
77
|
+
SERVICE_HOST = 'https://oss.aliyuncs.com'
|
|
78
|
+
#: ``endpoint`` is probed once per (access_key_id, bucket) and kept in memory: a
|
|
79
|
+
#: private backend asks for a read address on every image view.
|
|
80
|
+
_ENDPOINT_CACHE = {}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def hook_manifest():
|
|
84
|
+
return {
|
|
85
|
+
'title': '阿里云 OSS',
|
|
86
|
+
'catalog': 'upload',
|
|
87
|
+
'config_schema': [
|
|
88
|
+
{
|
|
89
|
+
'type': kind,
|
|
90
|
+
'key': key,
|
|
91
|
+
'label': label,
|
|
92
|
+
'placeholder': hint,
|
|
93
|
+
'required': key in REQUIRED,
|
|
94
|
+
}
|
|
95
|
+
for key, kind, label, hint in FIELDS
|
|
96
|
+
],
|
|
97
|
+
'actions': [
|
|
98
|
+
{
|
|
99
|
+
'name': 'ping',
|
|
100
|
+
'label': '检查存储空间',
|
|
101
|
+
'scope': 'admin',
|
|
102
|
+
'with_form': True,
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
'slots': [{'name': 'admin.hook_setting'}],
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def slot_admin_hook_setting(context=None, **kwargs):
|
|
110
|
+
backend = (context or {}).get('backend') or {}
|
|
111
|
+
config = backend.get('config') or {}
|
|
112
|
+
children = [
|
|
113
|
+
{
|
|
114
|
+
'type': kind,
|
|
115
|
+
'key': key,
|
|
116
|
+
'label': label,
|
|
117
|
+
'help': hint,
|
|
118
|
+
'default': config.get(key) or '',
|
|
119
|
+
'required': key in REQUIRED,
|
|
120
|
+
}
|
|
121
|
+
for key, kind, label, hint in FIELDS
|
|
122
|
+
]
|
|
123
|
+
children.append(
|
|
124
|
+
{'type': 'button', 'text': '检查存储空间', 'action': 'ping', 'with_form': True}
|
|
125
|
+
)
|
|
126
|
+
if backend.get('in_use'):
|
|
127
|
+
children.insert(
|
|
128
|
+
0, {'type': 'alert', 'level': 'success', 'text': '当前正在使用'}
|
|
129
|
+
)
|
|
130
|
+
return [{'type': 'fieldset', 'legend': '阿里云 OSS', 'children': children}]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _config(backend=None) -> dict:
|
|
134
|
+
return (backend or {}).get('config') or {}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _value(values, config, key) -> str:
|
|
138
|
+
value = (values or {}).get(key)
|
|
139
|
+
if value in (None, ''):
|
|
140
|
+
value = (config or {}).get(key)
|
|
141
|
+
return str(value or '').strip()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _key(basedir, upload_path, filename) -> str:
|
|
145
|
+
parts = []
|
|
146
|
+
for part in (basedir, upload_path, filename):
|
|
147
|
+
text = str(part or '').strip('/')
|
|
148
|
+
if text:
|
|
149
|
+
parts.append(text)
|
|
150
|
+
return '/'.join(parts)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _normalize_endpoint(endpoint) -> str:
|
|
154
|
+
text = str(endpoint or '').strip().rstrip('/')
|
|
155
|
+
if text and '://' not in text:
|
|
156
|
+
text = 'https://' + text
|
|
157
|
+
return text
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _public_domain(config: dict, backend=None) -> str:
|
|
161
|
+
"""Direct link prefix; empty for a ``private`` backend.
|
|
162
|
+
|
|
163
|
+
A private bucket rejects anonymous reads, so handing out ``src`` would give
|
|
164
|
+
the browser a 403. The service layer ignores it, but per the driver contract
|
|
165
|
+
the driver should not return one at all: private images are always served by
|
|
166
|
+
``/raw/<sha>`` through :func:`upimg_url`.
|
|
167
|
+
"""
|
|
168
|
+
visibility = str((backend or {}).get('visibility') or '').strip().lower()
|
|
169
|
+
if visibility == 'private':
|
|
170
|
+
return ''
|
|
171
|
+
return str(config.get('domain') or '').strip().rstrip('/')
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _auth(config: dict):
|
|
175
|
+
"""Return ``(Auth, "")`` or ``(None, error message)``."""
|
|
176
|
+
if Auth is None:
|
|
177
|
+
return None, 'Please install oss2 module'
|
|
178
|
+
for key in REQUIRED:
|
|
179
|
+
if not str(config.get(key) or '').strip():
|
|
180
|
+
return None, 'The aliyun %s parameter error' % key
|
|
181
|
+
return (
|
|
182
|
+
Auth(
|
|
183
|
+
str(config['access_key_id']).strip(),
|
|
184
|
+
str(config['secret_key']).strip(),
|
|
185
|
+
),
|
|
186
|
+
'',
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _detect_endpoint(config: dict, auth) -> str:
|
|
191
|
+
"""Endpoint of the configured bucket, empty when it cannot be probed."""
|
|
192
|
+
bucket = str(config.get('bucket') or '').strip()
|
|
193
|
+
cache_key = (str(config.get('access_key_id') or '').strip(), bucket)
|
|
194
|
+
if cache_key in _ENDPOINT_CACHE:
|
|
195
|
+
return _ENDPOINT_CACHE[cache_key]
|
|
196
|
+
|
|
197
|
+
endpoint = ''
|
|
198
|
+
try:
|
|
199
|
+
service = Service(auth, SERVICE_HOST)
|
|
200
|
+
for info in BucketIterator(service, prefix=bucket):
|
|
201
|
+
if info.name == bucket:
|
|
202
|
+
endpoint = _normalize_endpoint(info.extranet_endpoint)
|
|
203
|
+
break
|
|
204
|
+
except Exception:
|
|
205
|
+
logger.warning(
|
|
206
|
+
'Failed to detect the endpoint of OSS bucket %s', bucket, exc_info=True
|
|
207
|
+
)
|
|
208
|
+
if endpoint:
|
|
209
|
+
_ENDPOINT_CACHE[cache_key] = endpoint
|
|
210
|
+
return endpoint
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _bucket_client(config: dict):
|
|
214
|
+
"""Return ``(oss2.Bucket, "")`` or ``(None, error message)``."""
|
|
215
|
+
auth, error = _auth(config)
|
|
216
|
+
if error:
|
|
217
|
+
return None, error
|
|
218
|
+
endpoint = _normalize_endpoint(config.get('endpoint')) or _detect_endpoint(
|
|
219
|
+
config, auth
|
|
220
|
+
)
|
|
221
|
+
if not endpoint:
|
|
222
|
+
return None, 'The aliyun endpoint parameter error'
|
|
223
|
+
return Bucket(auth, endpoint, str(config['bucket']).strip()), ''
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _status_of(exc) -> int:
|
|
227
|
+
"""HTTP status carried by an ``oss2.exceptions.OssError``."""
|
|
228
|
+
for name in ('status', 'status_code'):
|
|
229
|
+
try:
|
|
230
|
+
return int(getattr(exc, name))
|
|
231
|
+
except (AttributeError, TypeError, ValueError):
|
|
232
|
+
continue
|
|
233
|
+
return 0
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def upimg_save(filename, stream, upload_path='', backend=None, **kwargs):
|
|
237
|
+
config = _config(backend)
|
|
238
|
+
if not filename or stream is None:
|
|
239
|
+
return {'code': 1, 'message': 'Parameter error'}
|
|
240
|
+
client, error = _bucket_client(config)
|
|
241
|
+
if error:
|
|
242
|
+
return {'code': 1, 'message': error}
|
|
243
|
+
|
|
244
|
+
data = stream if isinstance(stream, (bytes, bytearray)) else stream.read()
|
|
245
|
+
key = _key(config.get('basedir'), upload_path, filename)
|
|
246
|
+
try:
|
|
247
|
+
response = client.put_object(key, data)
|
|
248
|
+
except Exception as exc:
|
|
249
|
+
return {'code': 1, 'message': str(exc)}
|
|
250
|
+
|
|
251
|
+
etag = str(getattr(response, 'etag', '') or '').replace('"', '').strip()
|
|
252
|
+
result = {
|
|
253
|
+
'code': 0,
|
|
254
|
+
'path': key,
|
|
255
|
+
'size': len(data),
|
|
256
|
+
'etag': etag or sha256(data).hexdigest()[:32],
|
|
257
|
+
}
|
|
258
|
+
#: 私有桶不给直链:读取一律走 /raw 的签名转发。
|
|
259
|
+
domain = _public_domain(config, backend)
|
|
260
|
+
if domain:
|
|
261
|
+
result['src'] = '%s/%s' % (domain, key)
|
|
262
|
+
return result
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def upimg_url(save_result, filename='', backend=None, expires=300, **kwargs):
|
|
266
|
+
"""Presigned GET address for a ``private`` bucket.
|
|
267
|
+
|
|
268
|
+
Only called when the backend ``visibility`` is ``private``: sapic asks for a
|
|
269
|
+
temporary address and relays the body itself, so neither the signature nor
|
|
270
|
+
the AccessKey ever reach the browser. The address is signed against the
|
|
271
|
+
bucket Endpoint (not the CDN domain), because OSS signature authorisation is
|
|
272
|
+
validated by the origin.
|
|
273
|
+
"""
|
|
274
|
+
config = _config(backend)
|
|
275
|
+
key = str((save_result or {}).get('path') or '').strip()
|
|
276
|
+
if not key:
|
|
277
|
+
return {'code': 1, 'message': 'The object key is required'}
|
|
278
|
+
client, error = _bucket_client(config)
|
|
279
|
+
if error:
|
|
280
|
+
return {'code': 1, 'message': error}
|
|
281
|
+
try:
|
|
282
|
+
url = client.sign_url('GET', key, int(expires or 300), slash_safe=True)
|
|
283
|
+
except Exception as exc:
|
|
284
|
+
return {'code': 1, 'message': str(exc)}
|
|
285
|
+
return {'code': 0, 'url': url}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def upimg_delete(sha, upload_path, filename, basedir, save_result, backend=None, **kwargs):
|
|
289
|
+
config = _config(backend)
|
|
290
|
+
client, error = _bucket_client(config)
|
|
291
|
+
if error:
|
|
292
|
+
return {'code': 1, 'message': error}
|
|
293
|
+
|
|
294
|
+
save_result = save_result or {}
|
|
295
|
+
key = save_result.get('path') or _key(
|
|
296
|
+
config.get('basedir') or basedir, upload_path, filename
|
|
297
|
+
)
|
|
298
|
+
if not key:
|
|
299
|
+
return {'code': 1, 'message': 'The object key is required'}
|
|
300
|
+
try:
|
|
301
|
+
client.delete_object(key)
|
|
302
|
+
except Exception as exc:
|
|
303
|
+
#: 失败不要抛:核心按 code 判定,抛出会打断同批次的其它副本删除。
|
|
304
|
+
return {'code': 1, 'message': str(exc)}
|
|
305
|
+
return {'code': 0}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def action_ping(values=None, context=None, **kwargs):
|
|
309
|
+
config = ((context or {}).get('backend') or {}).get('config') or {}
|
|
310
|
+
merged = {key: _value(values, config, key) for key, _, _, _ in FIELDS}
|
|
311
|
+
client, error = _bucket_client(merged)
|
|
312
|
+
if error:
|
|
313
|
+
return {'code': 1, 'message': error}
|
|
314
|
+
|
|
315
|
+
#: 桶级请求:任何异常都说明桶不可用(403 凭证/权限,404 桶名或 Endpoint
|
|
316
|
+
#: 与桶地区不匹配),直接返回原因便于排查。
|
|
317
|
+
try:
|
|
318
|
+
client.get_bucket_info()
|
|
319
|
+
except Exception as exc:
|
|
320
|
+
status = _status_of(exc)
|
|
321
|
+
if status in (401, 403):
|
|
322
|
+
return {'code': 1, 'message': 'invalid access key or bucket permission'}
|
|
323
|
+
return {'code': 1, 'message': str(exc)[:200]}
|
|
324
|
+
return {'code': 0, 'toast': {'level': 'success', 'text': 'bucket is reachable'}}
|