Home > Backend Development > PHP Tutorial > Practical Guide to Connecting PHP and Qiniu Cloud Storage Interface

Practical Guide to Connecting PHP and Qiniu Cloud Storage Interface

WBOY
Release: 2023-07-05 13:56:01
Original
1142 people have browsed it

Practical Guide to Connecting PHP and Qiniu Cloud Storage Interface

Introduction:
Cloud storage has become one of the commonly used solutions in most website development. Qiniu Cloud Storage, as the leading domestic cloud storage service provider, is loved by many developers. This article will introduce how to use PHP to interface with Qiniu Cloud Storage, and provide relevant code examples.

1. Register a Qiniu cloud storage account and create a storage space
Before starting, we need to register a Qiniu cloud storage account and create a storage space. The registration URL is https://www.qiniu.com. After successful registration, log in to the Qiniu Cloud Storage Console. In the console, find the "Storage Space" option and click the "Create Storage Space" button to create a new storage space.

2. Install Qiniu Cloud Storage SDK
Qiniu Cloud provides PHP SDK, which we can install through Composer. Open the terminal, enter the root directory of the project, and execute the following command to install:

composer require qiniu/php-sdk

3. Obtain Qiniu Cloud Storage Access Key and Secret Key
In Before connecting the interface, we need to obtain the Access Key and Secret Key of Qiniu Cloud Storage to facilitate subsequent permission verification. In the Qiniu Cloud Storage console, find the "Personal Panel" in the left navigation bar, click to enter, and then you can see the Access Key and Secret Key under the API Keys column.

4. Upload files to Qiniu Cloud Storage
Next, we will learn how to use PHP code to upload files to Qiniu Cloud Storage. First, we need to introduce the Qiniu Cloud Storage SDK and initialize the configuration.

require_once 'vendor/autoload.php';

use QiniuAuth;
use QiniuStorageUploadManager;

$accessKey = 'your_access_key';
$secretKey = 'your_secret_key';

$auth = new Auth($accessKey, $secretKey);
Copy after login

In the above code, we introduced the Auth and UploadManager classes of Qiniu Cloud Storage, initialized an Auth object, and used Access Key and Secret Key for authentication.

Next, we need to define an upload function to implement the logic of file upload. The code is as follows:

function uploadFile($bucket, $key, $filePath)
{
    global $auth;
    
    $token = $auth->uploadToken($bucket);
    $uploadMgr = new UploadManager();
    list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
    
    if ($err !== null) {
        return false;
    } else {
        return true;
    }
}
Copy after login

In the above code, we first obtain the upload credentials based on the name of the storage space, and use the upload credentials, the file's saving path, and the file's local path to call the putFile method of UploadManager to upload the file. If the upload is successful, the function returns true; otherwise, it returns false.

Finally, we can call the uploadFile function to upload files to Qiniu Cloud Storage. The sample code is as follows:

$bucket = 'your_bucket_name';
$key = 'your_file_key';
$filePath = '/path/to/your/file';

if (uploadFile($bucket, $key, $filePath)) {
    echo '文件上传成功';
} else {
    echo '文件上传失败';
}
Copy after login

In the above code, we need to replace the $bucket variable with the name of your storage space, the $key variable with the file name you want to save (including the file path), and the $filePath variable with The absolute path to your local file. Then call the uploadFile function to upload the file, and determine whether the upload is successful based on the returned result.

5. Delete files on Qiniu Cloud Storage
In addition to uploading files, we can also use PHP code to delete files on Qiniu Cloud Storage. To delete a file, you need to provide the storage space name and file name of the file.

function deleteFile($bucket, $key)
{
    global $auth;
    
    $bucketMgr = new BucketManager($auth);
    $err = $bucketMgr->delete($bucket, $key);

    if ($err !== null) {
        return false;
    } else {
        return true;
    }
}
Copy after login

In the above code, we first initialize a BucketManager object and call the delete method to delete the specified file. If the deletion is successful, the function returns true; otherwise, it returns false.

The sample code for using the deleteFile function to delete a file is as follows:

$bucket = 'your_bucket_name';
$key = 'your_file_key';

if (deleteFile($bucket, $key)) {
    echo '文件删除成功';
} else {
    echo '文件删除失败';
}
Copy after login

In the above code, we need to replace the $bucket variable with the name of the storage space and the $key variable with the name of the file to be deleted. Then call the deleteFile function to delete the file, and judge whether the deletion is successful based on the returned result.

Conclusion:
Through the study of this article, you have learned how to use PHP to interface with Qiniu Cloud Storage, and realize the file upload and file deletion functions. I hope this article is helpful to your learning and use!

The above is the detailed content of Practical Guide to Connecting PHP and Qiniu Cloud Storage Interface. For more information, please follow other related articles on the PHP Chinese website!

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