Home  >  Article  >  Backend Development  >  Example of object storage and file transfer configuration in PHP Huawei Cloud API interface docking

Example of object storage and file transfer configuration in PHP Huawei Cloud API interface docking

王林
王林Original
2023-07-06 17:09:101365browse

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

  1. Introduce the required classes and namespace
require_once 'vendor/autoload.php';
use ObsObsClient;
  1. Create OBS client
$accessKey = 'YOUR_ACCESS_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$endpoint = 'YOUR_OBS_ENDPOINT';
$obsClient = new ObsClient([
    'key' => $accessKey,
    'secret' => $secretKey,
    'endpoint' => $endpoint,
]);
  1. Create Bucket
$bucketName = 'your-bucket-name';
$obsClient->createBucket(['Bucket' => $bucketName]);
  1. Upload file
$sourceFile = '/path/to/your/file.jpg';
$destFile = 'your-object-key.jpg';
$result = $obsClient->putObject([
    'Bucket' => $bucketName,
    'Key' => $destFile,
    'SourceFile' => $sourceFile,
]);
  1. Download file
$destFile = '/path/to/save/file.jpg';
$result = $obsClient->getObject([
    'Bucket' => $bucketName,
    'Key' => $destFile,
    'SaveAsFile' => $destFile,
]);
  1. Delete the file
$objectKey = 'your-object-key.jpg';
$result = $obsClient->deleteObject([
    'Bucket' => $bucketName,
    'Key' => $objectKey,
]);
  1. Close the OBS client
$obsClient->close();

4. File transfer example

  1. Introduce the required classes and naming Space
require_once 'vendor/autoload.php';
use HuaweiCloudSDKCoreExceptionSdkException;
use HuaweiCloudSDKOBS2RegionRegionEnum;
use HuaweiCloudSDKOBS2OBSClient;
  1. Create OBS client
$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,
]);
  1. Upload file
$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();
}
  1. Download file
$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();
}
  1. Delete files
$options = [
    'bucketName' => 'your-bucket-name',
    'objectKey' => 'your-object-key.jpg',
];
try {
    $obsClient->deleteObject($options);
} catch (SdkException $e) {
    echo $e->getMessage();
}
  1. Close OBS client
$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!

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