Uploading file to OBS in Huawei Cloud Services using PHP

0
418

Huawei Cloud is a cloud computing platform offered by Huawei, a leading global information and communications technology (ICT) solutions provider. It provides a range of cloud services, including computing, storage, databases, network, security, artificial intelligence, and Internet of Things (IoT), among others.

Huawei Cloud enables businesses of all sizes to build, deploy, and run their applications and services in the cloud, providing the scalability, reliability, and security that organizations require. With Huawei Cloud, you can get started quickly, with no upfront investment or long-term commitment, and only pay for what you use.

Huawei Cloud aims to provide a secure and reliable cloud computing platform for customers globally, with a focus on serving the needs of the Asian market.

To upload a file to Huawei OBS (Object Storage Service), you need to perform the following steps:

Steps to Upload File to Huawei OBS (Object Storage Service)

1. Log in to the Huawei Cloud Management Console and navigate to the OBS Service page.
2. Create a bucket, which is a container for storing your data.
3. Click the bucket to open it and then select “Upload Files”.
4. Select the file you want to upload and click “Open” to start the upload process.
5. Once the upload is complete, the file will be stored in the bucket and can be accessed from there.

Note: Huawei OBS may have specific requirements for file types and sizes, so make sure to check these before uploading your files.

Here is a sample PHP code that you can use to upload a file to Huawei OBS:

Sample PHP Script to upload Files to Huawei OBS (Object Storage Service)

<?php

// replace with your OBS access key and secret key
$accessKey = "YOUR_ACCESS_KEY";
$secretKey = "YOUR_SECRET_KEY";

// replace with your OBS endpoint and bucket name
$endpoint = "https://obs.cn-north-1.myhuaweicloud.com";
$bucket = "YOUR_BUCKET_NAME";

// replace with the path to your file
$filePath = "path/to/your/file.txt";

// create a request for the PUT object operation
$url = "$endpoint/$bucket/".basename($filePath);
$contentType = mime_content_type($filePath);
$date = gmdate('D, d M Y H:i:s T');
$signature = base64_encode(hash_hmac('sha1', "PUT\n\n$contentType\n$date\n/$bucket/".basename($filePath), $secretKey, true));

// upload the file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: $contentType",
    "Authorization: AWS $accessKey:$signature",
    "Date: $date",
]);
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
$response = curl_exec($ch);
curl_close($ch);

// check the response for success or failure
if ($response === false) {
    die("Failed to upload file: ".curl_error($ch));
} else if (substr($response, 0, 4) != "HTTP") {
    die("Failed to upload file: Unexpected response");
} else if (strpos($response, "200 OK") === false) {
    die("Failed to upload file: ".substr($response, 0, strpos($response, "\r\n")));
}

echo "File uploaded successfully";

 

 

 

Comments are closed.