Metadata-Version: 2.4
Name: up2s3
Version: 0.2.0
Summary: Save uploaded pictures in AWS S3.
Home-page: https://github.com/sapicd/up2s3
Author: staugur
Author-email: me@tcw.im
License: Apache 2.0
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: boto3
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license
Dynamic: license-file
Dynamic: requires-dist
Dynamic: summary

up2s3
========

这是基于 `sapic <https://github.com/sapicd/sapic>`_ 图床（v2）的一个扩展模块，
用来将上传的图片保存到 AWS `S3 <https://aws.amazon.com/s3/>`_\ （或任意 S3 兼容服务）。

- 钩子名（模块名）：``up2s3``
- 扩展类型：``upload``\ （存储驱动）
- 适用版本：sapic 2.x（V1 的 ``g.cfg`` / ``intpl_localhooksetting`` 契约已废弃）

安装
-----

- 正式版本

    `$ pip install -U up2s3`

- 开发版本

    `$ pip install -U git+https://github.com/sapicd/up2s3.git@master`

依赖 ``boto3``；未安装时钩子仍能加载，只是上传会返回
``Please install boto3 module``。

开始使用
---------

添加：
^^^^^^^^

在 **站点管理-钩子插件** 中导入模块名 ``up2s3``\ （或 ``POST /api/v2/hooks/import``），
然后启用它。启用后 sapic 会自动创建一条同名存储后端。

配置：
^^^^^^^^

在 **站点管理-存储后端** 里为该后端填写（不是旧的“网站设置”）：

- ``bucket``\ （必填）：S3 存储桶名称
- ``access_key_id``\ （必填）：AWS 访问密钥 ID，可用有 S3 权限的 IAM 子用户
- ``secret_key``\ （必填）：AWS 访问密钥密码
- ``region``：地域，如 ``ap-northeast-1``；留空则自动探测（需 ``s3:GetBucketLocation``）
- ``endpoint``：S3 兼容服务的 Endpoint，如 ``https://minio.example.com``；AWS 留空
- ``domain``：自定义域名/CloudFront 域名；留空则由 sapic 的 ``/raw/<sha>`` 代理输出
- ``acl``：上传时写入的对象 ACL（``不设置`` / ``public-read`` / ``private``），
  默认不设置，由桶策略决定。AWS 新版桶默认禁用 ACL，选 ``public-read`` 会报
  ``AccessControlListNotSupported``
- ``basedir``：存储根目录，默认桶根目录
- ``thumb_suffix``：缩略图参数，如 ``?width=400``

填好后可点「检查存储桶」验证凭证与桶是否可达。

直链规则：配了 ``domain`` 就用它；没配但 ``acl`` 是 ``public-read`` 且未使用自定义
``endpoint`` 时，回退到 ``https://<bucket>.s3.<region>.amazonaws.com/<key>``；其余
情况由 ``/raw/<sha>`` 输出。V1 会强制写入 ``public-read``，V2 改成按配置决定。

同一个驱动可以建多条存储后端（不同桶、不同密钥、不同优先级），上传走优先级
最高的那条，其余作为异步副本；判断依据是后端行的 ``priority``\ （数值越小越优先）。

私有桶（签名读）：
^^^^^^^^^^^^^^^^^^

桶为私有读时，把后端的「访问方式」设为 ``private`` 即可，``domain`` 可留空：

- 本驱动实现了 ``upimg_url``，后台不会再报 ``driver cannot read private objects``；
- 图片直链固定为 ``/raw/<sha>``，sapic 请求本驱动签发 ``generate_presigned_url`` 的
  临时地址（有效期由核心决定，默认 300 秒），在服务端拉取并流式转发，签名与凭证
  都不会出现在浏览器；
- 签名针对 S3 源站（或自定义 ``endpoint``），不针对 CloudFront/自定义域名；
- ``private`` 后端不再返回 ``src``，需要 CDN 直链的可同时建一个 ``public`` 后端副本。

V1 升级提示
------------

V2 的配置存在存储后端行里，V1 站点设置中的 ``s3_*`` 不会自动迁移，需要重新填写
一次；已上传图片的记录与旧直链不受影响。
