Home PHP Framework ThinkPHP How to use Qiniu Cloud Storage in ThinkPHP6

How to use Qiniu Cloud Storage in ThinkPHP6

Jun 20, 2023 pm 01:32 PM
thinkphp Tutorial Qiniu Cloud Storage

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:

1

composer require qiniu/php-sdk

Copy after login

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

1

'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:

1

2

3

4

5

6

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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!

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Samsung Galaxy Buds Pro Bluetooth headset pairing guide and usage tutorial Samsung Galaxy Buds Pro Bluetooth headset pairing guide and usage tutorial Dec 30, 2023 pm 03:44 PM

Samsung's Galaxy Buds series of headphones are uniquely designed and ergonomically designed, making them very comfortable to wear. In addition to being comfortable to wear, Samsung Buds series headphones also provide excellent sound quality experience, which has attracted many users. Next, we will introduce to you the usage tutorial of Samsung Buds headphones, especially the pairing process of Samsung Galaxy Buds Pro Bluetooth headphones and mobile phones. How to pair Samsung Galaxy Buds Pro Bluetooth headsets. Pairing with Samsung Buds headsets is very simple. Just follow the steps below to complete the pairing. The first step is to turn on the Bluetooth function of the phone. You can find the Bluetooth setting option in the settings interface of your phone. Step 2: Place GalaxyBu

How to use Aisi Assistant-Aisi Assistant usage tutorial How to use Aisi Assistant-Aisi Assistant usage tutorial Mar 06, 2024 pm 12:10 PM

Many novice friends still don’t know how to use Aisi Assistant, so the editor below has brought a tutorial on how to use Aisi Assistant. Friends who need it can quickly take a look. 1. Operation interface, check whether necessary components are installed (as shown in the figure below); 2. My device window, read the relevant description information (as shown in the figure below); 3. If the link cannot be made, according to Instructions for related operations (as shown in the figure below); 4. Use the game window to quickly complete the experience of related popular games (as shown in the figure below); 5. Download and select the various ringtones you need to use (as shown in the figure below) shown); 6. Quickly download various high-definition wallpapers (as shown in the figure below); 7. Support the function of rooting and jailbreaking (as shown in the figure below). above

How to use exescope? Detailed usage tutorial How to use exescope? Detailed usage tutorial Mar 13, 2024 pm 10:40 PM

exescope is a powerful program resource editing tool that can easily convert the English version of the software into the Chinese version. So how to use the newly downloaded exescope? If you are also a novice user, come and take a look at the detailed tutorial below. Exescope usage tutorial 1. Open eXeScope, click on the file in the upper left corner, click [Open] in the pop-up options, and select the file you want to open. 2. After opening the software, you can see many controls. 3. You can directly modify the size, position, and content of each control by inputting data, or you can directly click on the dialog box editor above and directly drag the controls on the program interface. After adjusting the controls, click [Save].

Little black box usage tutorial Little black box usage tutorial Feb 23, 2024 pm 05:10 PM

Little Black Box is a very easy to use game platform, so how to use this software specifically? Users will encounter many problems when using this software and have different solutions. This tutorial summary of the multi-functional use of the small black box will tell you how to use it. The following is a detailed introduction, so take a look! Tutorial on using Little Black Box [2024-01-31] ​​How to redeem the cdkey purchased from Little Black Box [2024-01-31] ​​How to store games purchased from Little Black Box into Steam [2024-01-31] ​​The CDkey purchased from Little Black Box can be returned Payment?

How to use Enterprise WeChat - Tutorial on using Enterprise WeChat How to use Enterprise WeChat - Tutorial on using Enterprise WeChat Mar 04, 2024 pm 02:28 PM

Many friends don’t know how to use Enterprise WeChat, so the editor below will share the tutorial for using Enterprise WeChat. Let’s take a look. I believe it will be helpful to everyone. Step 1: Click the "Enterprise WeChat" icon to enter Enterprise WeChat. In the message section, we can join the group chat to chat with colleagues (as shown in the picture). Step 2: On the main interface of Enterprise WeChat, click "Contact Book" (as shown in the picture). Step 3: Enter the address book section, where we can view our contacts (as shown in the picture). Step 4: On the main interface of Enterprise WeChat, click "Workbench" (as shown in the picture). Step 5: Enter the workbench section, where we can carry out the company's daily small work (as shown in the picture). Step 6: On the main interface of Enterprise WeChat, click "Me" (as shown in the picture

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to use Thunder magnet link - Tutorial on using Thunder magnet link How to use Thunder magnet link - Tutorial on using Thunder magnet link Mar 05, 2024 pm 05:22 PM

Recently, many friends have asked the editor how to use Thunder Magnet Links. Next, let us learn the tutorial on using Thunder Magnet Links. I hope it can help everyone. Step 1: First, we open the installed Thunder and click the New button, as shown in the picture. Step 2: Then find a magnet link on the Internet, copy and paste it to the location shown in the picture below, and click OK, as shown in the picture. Step 3: The detailed download directory of the resource pops up. We check the resource to be downloaded, select the storage directory, and click Download Now, as shown in the figure. Step 4: Finally, we wait for the resources to download, as shown in the picture. The above is all the content on how to use Thunder magnet links brought to you by the editor. I hope it can be helpful to everyone.

How to use ons simulator - ons simulator usage tutorial How to use ons simulator - ons simulator usage tutorial Mar 04, 2024 pm 01:10 PM

Recently, many friends have asked me how to use the ons simulator. Next, let us learn the tutorial on how to use the ons simulator. I hope it can help everyone. Step 1: First download the onscripter tool as shown below. Step 2: Then download the ons emulator game. Step 3: Then drag the simulator onscripter.exe file you just downloaded directly into the game directory and double-click onscripter.exe to open it. Step 4: Just run the game directly. The above is all the content on how to use the ons simulator brought by the editor. I hope it can be helpful to everyone.

See all articles