PHP implements open source Qiniu Cloud SDK

PHPz
Release: 2023-06-18 18:32:02
Original
1583 people have browsed it

With the development of cloud computing technology, more and more applications have begun to rely on cloud storage services, and Qiniu Cloud is one of the popular cloud storage service providers. When using Qiniu Cloud services, we need to use the SDK they provide to complete operations such as data upload and file management. The Qiniu Cloud SDK implemented in PHP has also attracted much attention from developers. This article will introduce how to use PHP to implement Qiniu Cloud SDK, and how to use it to upload and manage files.

  1. Install and configure Qiniu Cloud SDK

Before starting to use Qiniu Cloud SDK, we need to install it into our project first. The most common installation method is to use Composer and run the following command in the project root directory:

composer require qiniu/php-sdk
Copy after login

Composer will automatically add SDK dependencies to the project and download related files.

After successfully installing Qiniu Cloud SDK into the project, we need to configure it for use. When using Qiniu Cloud SDK, you need to use the Access Key and Secret Key for authentication, as well as the Bucket name, domain name and other information. We can configure the SDK by defining the following constants in the project's configuration file:

<?php
// 七牛云Access Key
define('QINIU_ACCESS_KEY', 'your access key here');

// 七牛云Secret Key
define('QINIU_SECRET_KEY', 'your secret key here');

// 默认Bucket名称
define('QINIU_BUCKET', 'your bucket name here');

// 默认Bucket对应的域名
define('QINIU_DOMAIN', 'your bucket domain here');
Copy after login
  1. File upload

It is very simple to use Qiniu Cloud SDK to upload files, just need It can be done in a few lines of code. Qiniu Cloud SDK provides two methods: putFile in QiniuUploadManager and put method in QiniuStorageUploadManager. In this article, the putFile method is used for uploading.

Add the following code to your PHP file to implement the file upload function.

<?php
require_once __DIR__ . '/vendor/autoload.php';   // 引入SDK自动加载文件
 
use QiniuAuth;
use QiniuStorageUploadManager;
 
$auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
$bucket = QINIU_BUCKET;
 
$uploadMgr = new UploadManager();
 
// 调用 UploadManager 的 putFile 方法进行文件上传
list($ret, $err) = $uploadMgr->putFile($auth->uploadToken($bucket), 'filename', '/path/to/local/file');
Copy after login

Among them, the second parameter filename is the name of the file uploaded to Qiniu Cloud, and the third parameter /path/to/local/file is the local file path. After uploading, the $ret variable will contain the file information in Qiniu Cloud, while the $err variable will contain error information. We can print these variables for debugging and error checking.

  1. File Management

Qiniu Cloud SDK also provides very convenient file management functions. We can query, obtain, delete and other operations on files through a set of methods provided in the QiniuStorageBucketManager class. The following is an example code:

Query all files:

<?php
require_once __DIR__ . '/vendor/autoload.php';   // 引入SDK自动加载文件
 
$accessKey = QINIU_ACCESS_KEY;
$secretKey = QINIU_SECRET_KEY;
$bucket = QINIU_BUCKET;
 
$auth = new QiniuAuth($accessKey, $secretKey);
$config = new QiniuConfig();
$config->useHttpsDomain = true;
$config->useCdnDomain = true;
 
$bucketManager = new QiniuStorageBucketManager($auth, $config);
list($iterms, $marker, $Err) = $bucketManager->listFiles($bucket, null, null, 1000, '');
 
if ($Err !== null) {
    die("query files failed: " . $Err->message());
}
 
var_dump($iterms);
Copy after login

Among them, the listFiles method is used to query all files under the specified Bucket, among which $bucket is the Bucket name, $marker is the mark, $limit is the number of records returned each time, and $prefix is the file prefix.

Get the specified file information:

<?php
require_once __DIR__ . '/vendor/autoload.php';   // 引入SDK自动加载文件
 
use QiniuAuth;
use QiniuStorageBucketManager;
 
$auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
$bucket = QINIU_BUCKET;
 
$bucketMgr = new BucketManager($auth);
list($ret, $err) = $bucketMgr->stat($bucket, 'filename');
 
if ($err !== null) {
    die("get file info failed: " . $err->message());
}
 
var_dump($ret);
Copy after login

Among them, the stat method is used to obtain the detailed information of the specified file on Qiniu Cloud.

Delete the specified file:

<?php
require_once __DIR__ . '/vendor/autoload.php';   // 引入SDK自动加载文件
 
use QiniuAuth;
use QiniuStorageBucketManager;
 
$auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
$bucket = QINIU_BUCKET;
 
$bucketMgr = new BucketManager($auth);
list($ret, $err) = $bucketMgr->delete($bucket, 'filename');
 
if ($err !== null) {
    die("delete file failed: " . $err->message());
}
 
echo "Delete file success:" . PHP_EOL;
var_dump($ret);
Copy after login

Among them, the delete method is used to delete the specified file in the specified Bucket.

The above are some basic contents of using PHP to implement Qiniu Cloud SDK. I hope it can be helpful to everyone. Using Qiniu Cloud SDK can greatly simplify the development and management of cloud storage services, allowing us to focus more on the business logic of the application.

The above is the detailed content of PHP implements open source Qiniu Cloud SDK. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!