Home > PHP Framework > ThinkPHP > body text

How to use Qiniu Cloud Storage in ThinkPHP6

WBOY
Release: 2023-06-20 13:32:52
Original
2298 people have browsed it

With the continuous development and optimization of cloud computing technology, cloud storage has become the storage method chosen by more and more enterprises and individuals. Among them, Qiniu Cloud Storage is favored by the majority of users for its high reliability, high availability and high cost performance. So, how to use Qiniu Cloud Storage in ThinkPHP6? This article will introduce it to you in detail.

1. Register a Qiniu Cloud account and create a storage space

First, we need to go to the Qiniu Cloud official website to register an account. After successful registration, we can create our own storage space in the "Object Storage" page. It should be noted here that in order to ensure the security of the service, it is recommended to set the public mode of the storage space to "Private".

2. Install Qiniu Cloud SDK

Before using Qiniu Cloud Storage, we need to install Qiniu Cloud SDK first. It can be installed through Composer and run the following code in the terminal:

composer require qiniu/php-sdk
Copy after login

After the installation is completed, we need to add in the config/autoload.php file:

'Qiniu' => 'Qiniu\Auth::autoload'
Copy after login

3. Configure Qiniu Cloud account information

In ThinkPHP6, we can make relevant configurations through files in the config directory. We create a new file under config and name it "qiniu.php". In this file, we need to perform the following configuration:

return [
    'accessKey' => '此处填写您的AccessKey',
    'secretKey' => '此处填写您的SecretKey',
    'bucket' => '此处填写您的存储空间名称',
    'domain' => '此处填写您的存储空间的外链域名'
];
Copy after login

Among them, AccessKey and SecretKey are the key information in the Qiniu Cloud account, which can be found in the key management in the personal center; bucket is the storage space Name; domain is the external link domain name of the storage space. Of course, in actual use, you need to replace all this information with your own information.

4. Upload files to Qiniu Cloud

After completing the above work, we can start to use Qiniu Cloud storage in our program. Suppose we need to upload a picture named "demo.jpg", we can write the code as follows:

use QiniuAuth;
use QiniuStorageUploadManager;

class Demo
{
    public function upload()
    {
        $accessKey = config('qiniu.accessKey');
        $secretKey = config('qiniu.secretKey');
        $auth = new Auth($accessKey, $secretKey);
        $bucket = config('qiniu.bucket');
        
        $uploadManager = new UploadManager();
        
        $file = '此处填写您要上传的文件路径';
        $key = '此处填写您上传文件在七牛云上的文件名';
        
        $token = $auth->uploadToken($bucket);
        list($ret, $error) = $uploadManager->putFile($token, $key, $file);

        if ($error !== null) {
            return '上传失败';
        } else {
            return '上传成功';
        }
    }
}
Copy after login

When uploading a file, we first need to use AccessKey and SecretKey to generate an authorization code, and then use The putFile method of UploadManager uploads files to Qiniu Cloud. Among them, $file is the path of the local file, and $key is the file name after the file is uploaded to Qiniu Cloud. The upload result will be returned through $ret and $error. By checking whether $error is null, you can determine whether the upload is successful.

5. Use external links to access uploaded files

After uploading files to Qiniu Cloud, we need to provide external links to access the files on other websites. We can use the following code to generate external links:

use QiniuAuth;

class Demo
{
    public function getURL()
    {
        $accessKey = config('qiniu.accessKey');
        $secretKey = config('qiniu.secretKey');
        $auth = new Auth($accessKey, $secretKey);
        
        $bucket = config('qiniu.bucket');
        $domain = config('qiniu.domain');
       
        $fileName = '此处填写您要访问的文件名';
        $baseUrl = 'http://' . $domain . '/' . $fileName;
       
        $signedUrl = $auth->privateDownloadUrl($baseUrl);
        
        return $signedUrl;
    }
}
Copy after login

When generating external links, we need to pass in the required AccessKey, SecretKey, and file name. When using the $auth->privateDownloadUrl($baseUrl) method to obtain external links, it will first check whether the space to which the file belongs is in "private" mode, and then use the key to sign. The final generated external link is the address stored in the $signedUrl variable. We can open this address in the browser to access the files stored on Qiniu Cloud.

Summary:

The above is how to use Qiniu Cloud Storage in ThinkPHP6. Through Qiniu Cloud Storage, we can upload files to the cloud to achieve efficient storage and transmission of data. In actual use, we need to reasonably set the access permissions of storage space to ensure data security. Hope this article is helpful to everyone.

The above is the detailed content of How to use Qiniu Cloud Storage in ThinkPHP6. 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