使用腾讯云对象存储作为 Ghost 博客图床

iNote-
  • cd 到 Ghost 安装目录

    cd /var/www/ghost
    
  • 安装Oss存储模块

    sudo npm install ghost-qcloud-cos
    
  • 创建存储文件夹(如果尚不存在)

    sudo mkdir -p content/adapters/storage
    
  • 设置文件夹权限

    sudo chown    user:   user content
    sudo chmod 775 content
    sudo chown    user:   user content/adapters
    sudo chmod 777 content/adapters
    sudo chown    user:   user content/adapters/storage
    sudo chmod 777 content/adapters/storage
    sudo chown    user:   user content/adapters/storage/cos-store.js
    sudo chown 775 content/adapters/storage/cos-store.js
    
  • 将模块文件复制到该文件夹

    sudo cp -vR node_modules/ghost-qcloud-cos content/adapters/storage/ghost-qcloud-cos
    
  • 创建一个名为“ cos-store.js”的脚本,内容如下:

    sudo touch  content/adapters/storage/cos-store.js
    sudo vim  content/adapters/storage/cos-store.js
    // 编辑以下内容
    module.exports = require('ghost-qcloud-cos');
    
    sudo touch  content/adapters/storage/qcloud-cos-store.js
    sudo vim  content/adapters/storage/qcloud-cos-store.js
    module.exports = require('ghost-qcloud-cos-store');
    
  • 配置 config.[env].json

    cd /var/www/ghost/
    
    sudo vim config.production.json
    {
      "storage": {
        "active": "cos-store",
        "cos-store": {
          "baseUrl": "Base Url(Allowed to be empty)",
          "SecretId": "Secret Id",
          "SecretKey": "Secret Key",
          "Bucket": "xxxx-123456",
          "Region": "ap-chengdu"
        }
      }
    }
    
  • 重启生效。

    ghost restart
    

    以上方式,OSS 中图片路径和图片名称都是写死的,如果要自定义安装路径和图片名,需要使用 Git 方式安装。

    • 从项目 Github 源 Fork 一份代码到自己仓库中。

    • 创建文件夹 storage 并 Clone 代码至 storage 中

      sudo mkdir -p content/adapters/storage
      sudo chown    user:   user content/adapters/storage
      sudo chmod 775 content/adapters/storage
      cd content/adapters/storage
      sudo git clone https://github.com/ZhelinCheng/ghost-qcloud-cos.git
      
    • ghost-qcloud-cos 下安装依赖

      cd ghost-qcloud-cos
      npm i
      
    • 如需自定义图片储存路径和图片名称,请修改index.js 第72/73行。

      return `/ghost/content/images/${YY}/${MM}/${file.name}`
      
    • 配置 config.[env].json

      {
        "storage": {
          "active": "ghost-qcloud-cos",
          "ghost-qcloud-cos": {
            "baseUrl": "Base Url(Allowed to be empty)",
            "SecretId": "Secret Id",
            "SecretKey": "Secret Key",
            "Bucket": "xxxx-123456",
            "Region": "ap-chengdu"
          }
        }
      }
      
    • 注意,使用 Git 模式安装,Favicon 不能正常显示,暂时没有处理办法

sudo find ./ ! -path “./versions/*” -type f -exec chmod 664 {} ;

npm uninstall ghost-qcloud-cos
sudo chown ubuntu:ubuntu /var/www/ghost
发表于2020-01-18, 更新于2024-07-24