Home > Article > Backend Development > Example of object storage and file transfer configuration in PHP Huawei Cloud API interface docking
Object storage and file transfer configuration example in PHP Huawei Cloud API interface docking
Introduction:
With the rapid development of cloud computing, cloud storage services have become the first choice for enterprises to obtain and store massive data method. As a leading cloud service provider, Huawei Cloud's Object Storage Service (OBS) provides high scalability, high reliability, and high security storage solutions. In this article, we will introduce in detail how to use PHP language to connect to Huawei Cloud's OBS service, and give corresponding code examples.
1. Configure Huawei Cloud API Key
Before using Huawei Cloud’s OBS service, we first need to configure the corresponding API key. You can create a key pair in the Huawei Cloud console. After creating the key pair, we will use the key pair to connect to the OBS service.
2. Install dependency packages
Before starting to write code, we need to install the corresponding dependency packages first. In PHP, we use Composer to manage dependency packages. Create a composer.json file in the root directory of the project and add the following content:
{ "require": { "huaweicloud/huaweicloud-sdk-php-obs": "2.9.4" } }
After saving the file, execute the following command in the terminal to install the required dependency packages:
composer install
Three , Object storage example
require_once 'vendor/autoload.php'; use ObsObsClient;
$accessKey = 'YOUR_ACCESS_KEY'; $secretKey = 'YOUR_SECRET_KEY'; $endpoint = 'YOUR_OBS_ENDPOINT'; $obsClient = new ObsClient([ 'key' => $accessKey, 'secret' => $secretKey, 'endpoint' => $endpoint, ]);
$bucketName = 'your-bucket-name'; $obsClient->createBucket(['Bucket' => $bucketName]);
$sourceFile = '/path/to/your/file.jpg'; $destFile = 'your-object-key.jpg'; $result = $obsClient->putObject([ 'Bucket' => $bucketName, 'Key' => $destFile, 'SourceFile' => $sourceFile, ]);
$destFile = '/path/to/save/file.jpg'; $result = $obsClient->getObject([ 'Bucket' => $bucketName, 'Key' => $destFile, 'SaveAsFile' => $destFile, ]);
$objectKey = 'your-object-key.jpg'; $result = $obsClient->deleteObject([ 'Bucket' => $bucketName, 'Key' => $objectKey, ]);
$obsClient->close();
4. File transfer example
require_once 'vendor/autoload.php'; use HuaweiCloudSDKCoreExceptionSdkException; use HuaweiCloudSDKOBS2RegionRegionEnum; use HuaweiCloudSDKOBS2OBSClient;
$ak = 'YOUR_ACCESS_KEY'; $sk = 'YOUR_SECRET_KEY'; $projectId = 'YOUR_PROJECT_ID'; $region = RegionEnum::{"your-region-enum-value"}; $obsClient = new OBSClient([ 'ak' => $ak, 'sk' => $sk, 'projectId' => $projectId, 'region' => $region, ]);
$sourceFile = '/path/to/your/file.jpg'; $destFile = 'your-object-key.jpg'; $options = [ 'bucketName' => 'your-bucket-name', 'objectKey' => $destFile, 'sourceFile' => $sourceFile, ]; try { $obsClient->putObject($options); } catch (SdkException $e) { echo $e->getMessage(); }
$destFile = '/path/to/save/file.jpg'; $options = [ 'bucketName' => 'your-bucket-name', 'objectKey' => 'your-object-key.jpg', 'saveAsFile' => $destFile, ]; try { $obsClient->getObject($options); } catch (SdkException $e) { echo $e->getMessage(); }
$options = [ 'bucketName' => 'your-bucket-name', 'objectKey' => 'your-object-key.jpg', ]; try { $obsClient->deleteObject($options); } catch (SdkException $e) { echo $e->getMessage(); }
$obsClient->shutdown();
Conclusion:
Through the above example code, We can see that the connection between PHP and Huawei Cloud OBS service is very simple. We only need to configure the corresponding API key, install the dependency package, and follow the steps in the sample code. At the same time, Huawei Cloud OBS service provides a rich API interface to meet various needs for object storage and file transfer. Developers can flexibly use these API interfaces according to actual business needs to improve application performance and user experience.
The above is the detailed content of Example of object storage and file transfer configuration in PHP Huawei Cloud API interface docking. For more information, please follow other related articles on the PHP Chinese website!