Performing a Form-based Upload
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.
Performing a form-based upload is to upload objects to a specified bucket in HTML form. The maximum size of an object is 5 GB.
You can call ObsClient->createPostSignature to generate request parameters for form-based upload. You can also perform a form-based according to the following procedure:
- Call ObsClient->createPostSignature to generate request parameters for authentication.
- Prepare an HTML form page.
- Enter the request parameters in the HTML page.
- Select a local file and upload it in form-based mode.
There are two request parameters generated:
- Policy, which corresponds to the policy field in the form
- Signature: which corresponds to the signature field in the form
The following sample code shows how to generate the parameters in a form-based upload request.
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
'endpoint' => 'https://your-endpoint',
'signature' => 'obs'
]);
// Generate a signature for form-based upload.
try {
$resp = $obsClient->createPostSignature([
// Set the validity period of the form-based upload request, in seconds.
'Expires' => 3600,
// Set form parameters.
'FormParams' => [
// Set the object ACL to public read.
'x-obs-acl' => ObsClient::AclPublicRead,
// Set the object MIME type.
'content-type' => 'text/plain',
]
]);
// Obtain the request parameters for the form-based upload.
printf("Policy:%s\n", $resp['Policy']);
printf("Signature:%s\n", $resp['Signature']);
} catch (ObsException $e) {
// If the operation fails, obtain the exception details.
printf("CreatePostSignatureFailed: Status:%d, Code:%s, Message:%s\n",
$e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
// Close ObsClient to release resources.
$obsClient->close();
}
Code of an HTML form example is as follows:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="https://bucketname.your-endpoint/" method="post" enctype="multipart/form-data"> Object key <!-- Object name --> <input type="text" name="key" value="objectname" /> <p> ACL <!-- Object ACL --> <input type="text" name="x-obs-acl" value="public-read" /> <p> Content-Type <!-- Object MIME type --> <input type="text" name="content-type" value="text/plain" /> <p> <!-- Base64 code of the policy --> <input type="hidden" name="policy" value="*** Provide your policy ***" /> <!-- AK --> <input type="hidden" name="AccessKeyId" value="*** Provide your access key ***"/> <!-- Signature information --> <input type="hidden" name="signature" value="*** Provide your signature ***"/> <input name="file" type="file" /> <input name="submit" value="Upload" type="submit" /> </form> </body> </html>
- Values of policy and signature in the HTML form are obtained from the returned results of ObsClient->createPostSignature.
- You can directly download the HTML form example: PostDemo.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot