search
HomePHP FrameworkThinkPHPHow to use Qiniu Cloud Storage in ThinkPHP6

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

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

'Qiniu' => 'Qiniu\Auth::autoload'

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' => '此处填写您的存储空间的外链域名'
];

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 '上传成功';
        }
    }
}

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;
    }
}

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!

Statement
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
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.