Why is object storage recommended
Object storage can be simply understood as a data pool for storing unstructured data such as pictures, audio, and videos. Compared with the host server, it has the characteristics of fast read and write speed and is conducive to sharing. Why do I recommend object storage? I summed up these six points.
1, the separation of activity and activity of website data, greatly improve the performance of web pages
In general, we recommend that the host server and object storage cooperate to store website data. The host server is mainly responsible for storing the dynamic data of the website, and the object store is used to store the static files of the website. Thus, the separation of activity and activity of the website can be realized. When the user visits a website, the data can be read from the host server and the object storage server synchronously, which can greatly improve the performance of the web page.
Ideally, it would also be used in conjunction with a content delivery network, which we’ll talk about later.
2, a separate file management interface, management site files and local computer as convenient
Whether it is Tencent Cloud, Ali Cloud, Qiniu cloud, object storage has a separate management console, Tencent cloud and Ali Cloud have a special computer client. You don’t have to open the website, you can use the object storage like using Baidu cloud disk to manage your website files, in addition to upload, download, preview and other common functions, but also directly in the object storage for image processing/media transcoding/data analysis.
3. In essence, it is a “distributed server with built-in large disk capacity”. The same file can be shared across domains
In nature, object storage is a distributed server with built-in large-capacity hard disks. Object storage has its own CPU, memory, network, and disk system, and is intelligent. In the same object storage data pool, different buckets can be created to store data of different websites without interfering with each other. Moreover, the same file can be referenced to different websites, which can effectively reduce data redundancy.
4, multiple storage nodes, support cross-region real-time synchronization, achieve remote DISASTER recovery
Assuming your images and other data are stored on your own server, you can only protect your data by periodically backing up your data. If the amount of data is large, each backup requires a large amount of time and occupies a large amount of disk space, which is not convenient to manage.
If your data is placed in an object store datapool and is associated with the website. The backup of static files can then be handed over to object storage. You only need to back up a few dynamic files on your site. Save time and effort.
For example, IF I use object storage, I store static files such as pictures of the website in the nearest node [Shenzhen]. However, in extreme cases, such as a sudden power failure in Shenzhen node, all data may be lost. But if I chose [Hangzhou] as the second storage node, and set the real-time incremental synchronization of data from [Shenzhen] node to [Hangzhou] node. So even if the data of the Node in Shenzhen is lost, my data in Hangzhou can still be used. When the power is restored, I will synchronize the data from the Hangzhou node back to the Shenzhen node. This is what we call remote disaster recovery.
5. Low cost, flexible resource expansion and payment on demand
At present, alibaba Cloud object storage (OSS) 40 GIGABytes of capacity a year is about 9 yuan. Tencent Cloud Object Storage (COS) has 50 GIGABytes of free capacity plus 10 gigabytes of free downlink traffic. Has been able to meet the use of most personal site needs. Unlike the server traffic is fixed, included in the cost of the server. It’s the same whether you use it or not. Object storage can be charged based on what you actually use.
6. Save server space
Why did you choose to leave that at the end? Because now the price of the server is affordable, the activity is also more. Most stationmaster already did not exist the problem that server space is not enough. And object storage is not a solution to the problem of running out of server space. But to combine the advantages of [block storage], [file storage], so as to achieve efficient file reading and writing and sharing. But it’s still a fact that saves server space, so let’s mention it.
How to use Qiniuyun
- The premise to prepare
- Apply for qiniu Cloud account (ignored);
- Enable the object storage function (0/10GB space, free);
- Create a bucket (space name);
- In key management, create a key and obtain AccessKey/SecretKey.
Integrate PHP-SDK
Seven Niuyun development documentation: developer.qiniu.com/kodo
Enter the CMD terminal, switch to the root directory of your deployment project, and run the following command: Composer require qiniu/php-sdk
(2) Install in the traditional way into Qiniuyun, download the official PHP-SDK, decompress the downloaded SDK and put it into the third library file of the project (Vendor or extend).
Fourth, actual combat development
namespace app\cms\controller;
// Introduce the Qniuyun SDK
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use app\cms\model\Banner as BannerModel;
class Banner extends Controller
{
// Add the carousel page
public function addBannerHtml()
{
return view('banner/add_banner');
}
/** * Rotation graph upload interface */
public function uploadBanner()
{
// Receive the value of the uploaded file
$file = $_FILES;
// Local file path
$localFilePath = $file['file'] ['tmp_name'];
// Intercept the file name extension
//$suffix = '.jpg';
$suffix = strtolower(substr($file['file'] ['name'],strpos($file['file'] ['name'].'. ')));
// Generate a unique file name and rename it (MD5 encrypted original file name + seconds + random number)
$fileName = md5($file['file'] ['name']) . date('s',time()) . rand(1.9999999);
$fileName. =$suffix;
// Upload the qiniu cloud business logic
$accessKey = ' '; // Go to the key management console to get the AK
$secretKey = ' ';// Go to the key management console to get the SK
$auth = new Auth($accessKey.$secretKey);
// Qiniuyun bucket name, according to their own actual fill in
$bucket = ' ';
// Generate the upload Token
$token = $auth->uploadToken($bucket);
// Build the UploadManager object
$uploadMgr = new UploadManager();
// Call the putFile method of UploadManager to upload the file.
list($ret.$err) = $uploadMgr->putFile($token.$fileName.$localFilePath);
// Error message
if ($err! =null) {
// Can be adjusted to the error page
$this->error('Failed to upload file');
}
// Store qiniuyun image path to our own database qiniuyun Image path
$imageUrl = 'http://qtpud69oi.hn-bkt.clouddn.com/' . $fileName;
Only these two model methods can automatically write timestamp
$result =BannerModel::create([
'image_url'= >$imageUrl,]);$this->success('File uploaded successfully');
}
/** * Rotation chart page */
public function bannerList()
{
// Query user data with status 1 and display 10 pieces of data per page
$list = BannerModel::where('status'.1)
->paginate(3);
// Render template output
return view('banner/get_list',compact('list'));
}
/** * Delete the rotograph interface */
public function deleteBanner()
{
// Receive parameters
$params = input();
// Delete logic, delete qniuyun, delete database data
$banner = BannerModel::get($params['id']);
// Delete Qiniuyun
$accessKey = ' ';// Go to the key management console to get the AK
$secretKey = ' ';// Go to the key management console to get the SK
// Qiniuyun bucket name, according to their own actual fill in
$bucket = ' ';
// The domain name length
$len = strlen('http://qtpud69oi.hn-bkt.clouddn.com/');
// To delete the image path of Qiniuyun
$delImageUrl = substr($banner->image_url,$len);
$auth = new Auth($accessKey.$secretKey);
$config = new \Qiniu\Config();
$bucketManager = new \Qiniu\Storage\BucketManager($auth.$config);
$bucketManager->delete($bucket.$delImageUrl);
// Delete data
$banner->delete();
$this->success('File deleted successfully'); }}Copy the code
Finally, wechat search: Ke Zuo
There is a problem with the public feedback