Synchronous processing (x-oss-process) requires a program to wait for a task to complete before executing other tasks. You can use PHP SDK V2 for synchronous processing in scenarios such as image processing and document processing.
Precautions
-
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about region-to-endpoint mappings, see OSS regions and endpoints.
Sample code
The following example uses PHP SDK V2 to scale an image and save the processed image to a specified bucket for persistence.
<?php
// Import the autoloader file to load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located. This parameter is required.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint that other services can use to access OSS. This parameter is optional.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket. This parameter is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The name of the object. This parameter is required.
];
// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check whether required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required parameter is missing.
exit(1);
}
}
// Obtain the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"]; // The name of the object.
// Load credentials, such as AccessKeyId and AccessKeySecret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Construct an image processing command to resize the image width to 100 and save the image as a new object.
$process = sprintf("image/resize,w_100|sys/saveas,o_%s",
base64_encode($key) // Base64-encode the object name.
);
// Create a request object to process the object.
$request = new Oss\Models\ProcessObjectRequest(
bucket: $bucket, // The name of the bucket.
key: $key // The name of the object.
);
// Set the processing command.
$request->process = $process;
// Call the processObject method to process the object.
$result = $client->processObject($request);
// Print the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'process result:' . var_export($result, true) . PHP_EOL // The processing result.
);
Scenarios
References
-
For more information about image processing parameters, see Image processing parameters.
-
For more information about the synchronous processing feature, see Synchronous processing.