Home > php教程 > php手册 > Store pictures and article content in Alibaba Cloud OSS to reduce server space usage

Store pictures and article content in Alibaba Cloud OSS to reduce server space usage

WBOY
Release: 2016-10-17 09:12:02
Original
1645 people have browsed it

Content websites such as news and media publish a lot of text content. If it is stored directly in the database, it will occupy a very large amount of database. The same website also has a lot of picture resources, which takes up a lot of server space resources. So consider saving the content elsewhere and save the saved address into the article field of the database. This reduces the usage of the database, saves the pictures in the cloud disk, and references them from the cloud disk, thereby saving server hard disk space.
News, media and other content websites publish a lot of text content. If they are stored directly in the database, the database will be very large. The same website also has a lot of image resources, which takes up a lot of server space resources. So consider saving the content elsewhere and save the saved address into the article field of the database. This reduces the occupation of the database, saves the pictures in the cloud disk, and references them from the cloud disk, thereby saving server hard disk space.
The main idea of ​​this article is to separate the images that occupy server resources and the article content that occupies database resources from the server, save them to Alibaba Cloud OSS, and then return the save address in Alibaba Cloud and save them to the database. When using it, take out the pictures and data directly according to the path of Alibaba Cloud.
The implementation process is as shown below:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
Implementation steps:
1. Obtain the Access Key ID and Access Key Secret of Alibaba Cloud OSS. (The price of Alibaba Cloud OSS 40G cloud disk is 9 yuan per year, which is very cost-effective).
Log in to the Alibaba Cloud user management system: https://ak-console.aliyun.com/#/accesskey, and obtain the appkey and appsecret parameters on the following interface.
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
2. Create a bucket and set the properties to public read and write permissions. And obtain the OSS external domain name of the bucket, as shown below:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
3. Download the SDK file package of Alibaba Cloud OSS, download address: www.zixuephp.cn/xxxxxx. Configure the SDK file package Alioss folder to ThinkPHP's /ThinkPHP/Extend/Vendor/ folder.
4. Configure the obtained Alibaba Cloud OSS parameters Access Key ID and Access Key Secret into the project's config.php file. The configured parameters are as follows:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
5. Define Alibaba Cloud operation functions in the common function file common.php under the common folder: including functions for instantiating OSS objects, uploading images to Alibaba Cloud, deleting objects in Alibaba Cloud, and obtaining object paths in Alibaba Cloud. , object upload Alibaba Cloud function. The picture shows the method of instantiating Alibaba Cloud OSS:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
6. After submitting data from the background add content page, the Action processing method and saving to the database are as follows:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
7. In the front-end page, retrieve the Alibaba Cloud storage path of the content from the database, parse the content corresponding to the path, and display it in the front-end as follows:
Store pictures and article content in Alibaba Cloud OSS to reduce server space usage
These are the entire steps to save published graphic content to Alibaba Cloud.
The article is reproduced from: Self-study PHP blog http://www.zixuephp.cn
Download the source program code address of this case: http://t.cn/RVxAgUW

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template