2025年3月12日更新

事实证明,原来的方法是错误的。设置完之后,书籍封面的图片地址确实成功转换成OSS地址,但是页面中的图片不但没改变,而且新建页面时也无法正常上传图片。

查看错误日志,其中显示的上传地址是https://oss-cn-hangzhou.aliyuncs.com/bucket-test0001/uploads/images/,而这种写法在阿里云的OSS中是不被允许的,会提示“The bucket you are attempting to access must be addressed using OSS third level domain.”错误。如果增加了STORAGE_S3_REGION=oss-cn-hangzhou,上传地址又变成了https://s3-oss-cn-hangzhou.amazonaws.com//bucket-test0001/uploads/images/

搜索源码,其中ImageStorage.php文件中有如下代码:

/**
 * Get the public base URL used for images.
 * Will not include any path element of the image file, just the base part
 * from where the path is then expected to start from.
 * If s3-style store is in use it will default to guessing a public bucket URL.
 */
protected static function getPublicBaseUrl(): string
{
    $storageUrl = config('filesystems.url');

    // Get the standard public s3 url if s3 is set as storage type
    // Uses the nice, short URL if bucket name has no periods in otherwise the longer
    // region-based url will be used to prevent http issues.
    if (!$storageUrl && config('filesystems.images') === 's3') {
        $storageDetails = config('filesystems.disks.s3');
        if (!str_contains($storageDetails['bucket'], '.')) {
            $storageUrl = 'https://' . $storageDetails['bucket'] . '.s3.amazonaws.com';
        } else {
            $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
        }
    }

    $basePath = $storageUrl ?: url('/');

    return rtrim($basePath, '/');
}

将其中的s3.amazonaws.com替换成aliyuncs.com,结果还是一样。无奈之下只能将之前在数据库中修改的所有图片链接重新由OSS域名改回了网站域名。

就在放弃前的最后一次尝试中,我注释掉了STORAGE_TYPE=s3,其他参数保持不变。图片竟然上传成功了,而且OSS和本地都保存了一份。

但如果我删除某个页面并清空回收站,然后使用清理图像功能,本地图片会被删除,而保存在OSS上的图片却依旧存在,不知道是不是系统设计还是我配置的问题。

另外,如果不手动修改数据库的话,新上传的文件将使用OSS域名,而之前的文件使用的还是网站域名,但不影响使用。

2025年3月10日原文

既然MediaWiki都已经用上阿里云的OSS了,那剩下的BookStack也可以考虑上了。

从官方文档给出的内容来看,确实有实现的可能性,毕竟阿里云的OSS兼容亚马逊的S3(申请OSS以及账户的方法见《如何在MediaWiki站点中使用阿里云OSS存储上传文件》一文)。

打开配置文件.env,然后添加如下内容(//后的内容为方便记录而添加的注释,实际环境中并不支持该格式):

STORAGE_TYPE=s3 //默认不用改
STORAGE_S3_KEY=AccessKey ID //阿里云的AccessKey ID
STORAGE_S3_SECRET=AccessKey Secret //阿里云的AccessKey Secret
STORAGE_S3_BUCKET=bucket-test0001 //bucket名称
STORAGE_S3_REGION=oss-cn-hangzhou //bucket所处的区域(比如杭州)
STORAGE_S3_ENDPOINT=https://oss-cn-hangzhou.aliyuncs.com //填写bucket所处的endpoint
STORAGE_URL=bucket-test0001.oss-cn-hangzhou.aliyuncs.com //bucket的域名,如果开启了OSS的CDN则可以使用CDN域名

如果是已有站点并上传过部分照片,那么记得将图片文件同步到OSS中,命令如下:

ossutil sync /var/www/html/bookstack/public/uploads/images/  oss://bucket-test0001/uploads/images/

其中,/var/www/html/bookstack为BookStack所在的根目录。

刷新一下页面,检查一下图片的链接是否已经切换到OSS的URL上了。

注意:如果之前上传过图片并保存于本地,后切换成OSS的,那么除了书籍封面外,章节和页面中的图片可能需要手动修改一下(系统并不会自动修改。
进入数据库,先以https://你的域名/uploads/images进行搜索,找到对应的表和字段,然后替换成https://你的OSS域名/uploads/images,比如:

UPDATE `images` SET `url` = REPLACE( `url`, 'https://你的域名/uploads', 'https://你的OSS域名/uploads' );
UPDATE `pages` SET `html` = REPLACE( `html`, 'https://你的域名/uploads', 'https://你的OSS域名/uploads' );
UPDATE `page_revisions` SET `html` = REPLACE( `html`, 'https://你的域名/uploads', 'https://你的OSS域名/uploads' );
UPDATE `settings` SET `value` = REPLACE( `value`, 'https://你的域名/uploads', 'https://你的OSS域名/uploads' );

像我这种从http过来的,可能还要替换http://你的域名/uploads/images

参考资料:https://www.bookstackapp.com/docs/admin/upload-config/

最后修改:2025 年 03 月 13 日
如果觉得我的文章对你有用,请随意赞赏