Recently, I came across a image-sharing platform, so out of necessity, I decided to use Object Storage.
After reviewing the official documentation, I've finally been able to create what I needed – so, let me summarize it here!
How do I upload files to Qiniu Cloud Object Storage?
First, integrate the official SDK files into your project.
Then, PHP is introduced – you can write the code directly. Due to the confidentiality requirements of our business, I am only making the code for the upload functionality public!
<?php
require './vendor/autoload.php';
require('../action/class/common.php');
$file = glob('./up/*.png');
$filename = basename($file[0]);
$locfile = $file[0];
$yunfilenam = 'score/'.$filename;
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
// 用于签名的公钥和私钥
$accessKey = '';
$secretKey = '';
$bucket = '';
// 初始化签权对象
$auth = new Auth($accessKey, $secretKey);
// 计算文件大小,生成压缩率
$filesize = filesize($locfile); // 获取文件大小,单位为字节
$targetSize = 100 * 1024;
if ($filesize < 100 * 1024 && $filesize > 80 * 1024) {
$water = 75; // 文件小于100KB,设置$water为75
} else {
$water = intval(($targetSize / $filesize) * 100);
}
$expires = 3600;
//持久化数据处理的上传凭证
$saveJpgEntry = \Qiniu\base64_urlSafeEncode($bucket . ":{$yunfilenam}");
$vframeJpgFop = "imageView2/0/format/jpg/q/{$water}|imageslim|saveas/" . $saveJpgEntry;
$policy = array(
'scope' => $bucket . ':' . $filename,
'persistentOps' => $vframeJpgFop,
'insertOnly' => 0,
'persistentPipeline' => "",
);
$token = $auth->uploadToken($bucket, null, $expires, $policy, true);
// print_r($token);die;
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $yunfilenam, $locfile, null, 'application/octet-stream', true, null, 'v2');
echo "\n====> putFile result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}Code Explanation:
This code is a PHP script that first includes two class library files:autoload.php和common.phpThese library files may implement certain basic functionalities. Then, use them.glob()Function selected./up/All items in the directory.pngSelect the filename of the first file and store it in a variable.$filenameMiddle.
Next, the image upload functionality is implemented using the Qiniu Cloud SDK. First, you need to prepare information such as your Qiniu Cloud access keys and bucket name; then, initialize the SDK.AuthObject – Generate Upload Credential$tokenThe compression rate is set based on the file size.$waterAnd the upload parameters are encapsulated into an array.$policyThis includes spatial names, file names, persistent data processing, etc. – final invocation.UploadManagerObjectputFile()Use the method to upload the file and determine whether the upload was successful based on the returned result. If the upload is successful, insert the file name into the MySQL database and transfer the file../up/Table of Contents →./bak/Output the table of contents and the operation results.
In summary, this code is designed to perform the following:./up/The first item in the table of contents.pngThe file is uploaded to Qiniu Cloud Space and compressed; simultaneously, the file name is inserted into the MySQL database; finally, the file is moved to./bak/catalogue.
How can I compress images when uploading a file?
In the above code:
$expires = 3600;
//持久化数据处理的上传凭证
$saveJpgEntry = \Qiniu\base64_urlSafeEncode($bucket . ":{$yunfilenam}");
$vframeJpgFop = "imageView2/0/format/jpg/q/{$water}|imageslim|saveas/" . $saveJpgEntry;
$policy = array(
'scope' => $bucket . ':' . $filename,
'persistentOps' => $vframeJpgFop,
'insertOnly' => 0,
'persistentPipeline' => "",
);This section involves processing the image – compressing it and then overwriting it.