With the advent of the cloud computing era, more and more companies and individuals are beginning to store data in the cloud. Qiniu Cloud is a leading cloud storage service provider in China, providing efficient, safe and reliable cloud storage services. In this article, we will introduce how to implement Qiniu cloud storage using PHP.
1: Register a Qiniu Cloud account and create a storage space
Before starting to use Qiniu Cloud storage, we need to register a Qiniu Cloud account and create a storage space.
1.1 Register Qiniu Cloud account
First, we need to go to Qiniu Cloud official website (https://www.qiniu.com/) and click the "Register" button in the upper right corner. Fill in your personal information to register.
1.2 Create a storage space
Log in to Qiniu Cloud Console, click "Storage Space", and then select "New". Enter your storage space name, select the storage area and storage type, and click Create.
2: Obtain the AccessKey and SecretKey of Qiniu Cloud Storage
Before using PHP to implement Qiniu Cloud Storage, we need to obtain the AccessKey and SecretKey for authentication and authorization.
2.1 Log in to Qiniu Cloud Console
Log in to Qiniu Cloud Console with the registered Qiniu Cloud account, click "Personal Center", and select "Key Management" from the left menu .
2.2 Obtain AccessKey and SecretKey
On the secret key management page, we can see AccessKey and SecretKey. These two keys are used for authentication and authorization, so be sure to keep them in a safe place.
3: Install Qiniu Cloud PHP SDK
To use Qiniu Cloud Storage in PHP, you need to install the SDK first. Qiniuyun PHP SDK can be installed through Composer.
Enter the following command in the command line window:
composer require qiniu/php-sdk
Four: Upload files to Qiniu Cloud Storage
Get the AccessKey and SecretKey of Qiniu Cloud Storage and install it After installing the SDK, we can use PHP to upload files to Qiniu Cloud Storage.
require_onceDIR. '/vendor/autoload.php';
$accessKey = 'ACCESS_KEY';
$secretKey = 'SECRET_KEY';
$bucket = 'BUCKET_NAME';
$filePath = '/path/to/local/file';
$key = 'remote_file_name';
// Construct authentication object
$auth = new \Qiniu\Auth($accessKey, $secretKey);
// Generate upload Token
$token = $auth->uploadToken($ bucket);
// Initialize the UploadManager object and upload files.
$uploadMgr = new \Qiniu\Storage\UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
echo "\n====> putFile result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
?>
It should be noted that before using the above code, you need to replace the $accessKey, $secretKey, $bucket, $filePath, $key and other parameters.
5: Get the list of uploaded files
Through the above method, we can upload local files to Qiniu Cloud Storage. As files are uploaded, we may need to get a list of files that have been uploaded. This can be achieved through Qiniu Cloud’s SDK. The following is the code to get a list of all files in a specified space.
require_onceDIR. '/vendor/autoload.php';
$accessKey = 'ACCESS_KEY';
$secretKey = 'SECRET_KEY';
$bucket = 'BUCKET_NAME';
$config = new \Qiniu\Config();
$config->useHTTPS = true;
/ / Manage credentials
$auth = new \Qiniu\Auth($accessKey, $secretKey);
// Build Qiniu cloud storage object
$client = new \Qiniu\Storage\BucketManager($auth, $config);
// File prefix
$prefix = '';
// List files
list($items, $marker, $err) = $client->listFiles($ bucket, $prefix);
if ($err !== null) {
echo "\n====> list file err: \n"; var_dump($err);
} else {
echo "\n====> list file result: \n"; var_dump($items);
}
?>
It should be noted that before using the above code, you need to replace parameters such as $accessKey, $secretKey, and $bucket.
6: Summary
By using Qiniu Cloud PHP SDK, we can easily upload files to Qiniu Cloud Storage and obtain the list of uploaded files. Of course, Qiniu Cloud Storage provides far more than these functions. Next, you can learn more about how to use Qiniu cloud storage by reading Qiniu official documentation.
The above is the detailed content of How to implement Qiniu cloud storage in php. For more information, please follow other related articles on the PHP Chinese website!