Overview
Ghost is a Node.js-based framework for quickly building a blog website. You can use its official CLI tool to quickly generate your personal website and deploy it in CVM or Docker. As a blog website, attachment upload is a necessary feature. Ghost stores attachments locally by default. This document describes how to save an attachment in COS through the plugin. Saving forum attachments in COS has the following benefits: Higher reliability for your attachments.
No need to prepare additional storage capacity on your server for forum attachments.
Faster access to image attachments through the COS server rather than taking up downstream bandwidth/increasing the traffic on your own server.
Accelerated forum user access to image attachments through CDN. Preparations
Building a Ghost website
1. Install the Node.js environment. 2. Install ghost-cli.
npm install ghost-cli@latest -g
3. Create a project and run the following command in the project's root directory:
After successful creation, the project structure is as shown below:
4. Open the browser and access localhost:2368
. On the sign-up page, sign up for an account and go to the management backend.
Creating a COS bucket
2. Click Security Management > CORS (Cross-Origin Resource Sharing) and add a CORS configuration as instructed in Setting Cross-Origin Resource Sharing (CORS). You can use the following configuration to facilitate debugging: Associating Ghost with a COS Bucket
1. Add the following configuration to the config.development.json
configuration file in the Ghost project's root directory:
"storage": {
"active": "ghost-cos-store",
"ghost-cos-store": {
"BasePath": "ghost/",
"SecretId": "AKID*************",
"SecretKey": "***************",
"Bucket": "xxx-125********",
"Region": "**-*******"
}
}
The parameters are described as follows:
|
BasePath | The COS path where files are stored. You can modify it as needed. If you leave it empty, the root directory will be used by default. |
SecretId | Enter the access key information, which can be created and obtained on the Manage API Key page. |
SecretKey | Your access key information, which can be created and obtained on the Manage API Key page. |
Bucket | The name customized during bucket creation such as `examplebucket-1250000000`. |
Region | The region selected during bucket creation |
2. Create a custom storage directory and run the following command in the project's root directory:
mkdir -p content/adapters/storage
3.1 Install it through npm.
npm install ghost-cos-store
3.2 Create the ghost-cos-store.js
file with the following content in the storage
directory:
module.exports = require('ghost-cos-store');
3.3 Run git clone
for installation.
cd content/adapters/storage
git clone https://github.com/tencentyun/ghost-cos-store.git
cd ghost-cos-store
npm i
3.4 After the installation, restart Ghost.
Publishing a Post and Testing Upload
1. In the Ghost console, click + to publish a post.
2. Click + to upload an image. From the packets captured by the browser, you can see that the upload
request succeeds and the COS URL of the image is returned.
Apakah halaman ini membantu?