Asynchronous processing (x-oss-async-process) allows a program to execute other tasks without waiting for the current one to complete. This topic describes how to use PHP SDK V2 for asynchronous processing in scenarios such as document conversion, video transcoding, and video merging.
Precautions
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouand the public endpoint by default. If you want to access OSS from other Alibaba Cloud services within the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.
Sample code
The following sample code provides an example of how to convert a document to a specific format.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Define the video processing style to convert the video to the specified format.
$style = "video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1";
// Construct an asynchronous processing instruction that includes the Base64-encoded bucket name and object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s",
$style,
rtrim(base64_encode($bucket), '='), // Base64-encode the bucket name and remove the equal sign (=) at the end.
rtrim(base64_encode($key), '=') // Base64-encode the object name and remove the equal sign (=) at the end.
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucket, // The name of the bucket.
key: $key // The name of the object.
);
// Set the processing instruction.
$request->process = $process;
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Scenarios
Video transcoding
You can use the video transcoding feature to change the encoding format of a video, reduce the video file size by lowering the resolution and bitrate, and convert the container format of the video.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Specify the name of the transcoded video.
$targetObject = "dest.avi";
// Define the processing style, including parameters such as the format, video codec, resolution, bitrate, frame rate, audio codec, and audio bitrate.
$style = "video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1";
// Construct an asynchronous processing instruction that includes the processing style and the save location of the processed file (the bucket and object names are Base64-encoded).
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s",
$style,
rtrim(base64_encode($bucketName), '='), // Base64-encode the bucket name and remove the trailing equal sign (=).
rtrim(base64_encode($targetObject), '=') // Base64-encode the object name and remove the trailing equal sign (=).
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $objectName, // The name of the object.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video-to-animated-image conversion
You can use the video-to-animated-image feature to convert a video to an animated image in a format such as GIF or WebP.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Specify the name of the processed animated GIF file.
$targetKey = "destexample.gif";
// Define the parameters for converting a video to an animated GIF, including the GIF width, height, and frame interval.
$animationStyle = "video/animation,f_gif,w_100,h_100,inter_1000";
// Construct an asynchronous processing instruction that includes the save path and the Base64-encoded bucket name and target file name.
$bucketNameEncoded = base64_encode($bucketName); // Base64-encode the bucket name.
$targetKeyEncoded = base64_encode($targetKey); // Base64-encode the target file name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $objectName, // The name of the object.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video-to-sprite conversion
You can use the video-to-sprite feature to extract video frames and combine them into a sprite based on specific rules.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Specify the name of the output sprite file.
$targetKey = "example.jpg";
// Construct the parameters for converting a video to a sprite, including the format, width, height, and frame interval.
$animationStyle = "video/sprite,f_jpg,sw_100,sh_100,inter_10000,tw_10,th_10,pad_0,margin_0";
// Construct an asynchronous processing instruction that includes the save path and the Base64-encoded bucket name and target file name.
$bucketNameEncoded = base64_encode($bucketName); // Base64-encode the bucket name.
$targetKeyEncoded = base64_encode($targetKey); // Base64-encode the target file name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $objectName, // The name of the object.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video snapshot
You can use the video snapshot feature to extract video frames based on specific rules and convert them to the required image format.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Specify the name of the file processed by video snapshot.
$targetKey = "dest.png";
// Construct the video snapshot parameters, including the format, width, height, cropping method, and frame interval.
$animationStyle = "video/snapshots,f_jpg,w_100,h_100,scaletype_crop,inter_10000";
// Construct an asynchronous processing instruction that includes the save path and the Base64-encoded bucket name and target file name.
$bucketNameEncoded = base64_encode($bucketName); // Base64-encode the bucket name.
$targetKeyEncoded = base64_encode($targetKey); // Base64-encode the target file name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $objectName, // The name of the object.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video merging
You can use the video merging feature to combine multiple videos into a single video and convert it to the required format.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Enter the name of the merged video file.
$targetObject = "dest.mp4";
// Specify the names of the video files to be merged.
$video1 = "concat1.mp4";
$video2 = "concat2.mp4";
// Construct the style string for video processing and the video merging parameters.
$style = sprintf(
"video/concat,ss_0,f_mp4,vcodec_h264,fps_25,vb_1000000,acodec_aac,ab_96000,ar_48000,ac_2,align_1/pre,o_%s/sur,o_%s,t_0",
rtrim(base64_encode($video1), '='), // Base64-encode the name of the first video file and remove the trailing equal sign (=).
rtrim(base64_encode($video2), '=') // Base64-encode the name of the second video file and remove the trailing equal sign (=).
);
// Construct the asynchronous processing instruction.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$style,
rtrim(base64_encode($bucketName), '='), // Base64-encode the bucket name and remove the trailing equal sign (=).
rtrim(base64_encode($targetObject), '=') // Base64-encode the target file name and remove the trailing equal sign (=).
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Audio transcoding
You can use the audio transcoding feature to convert an audio file to the required format.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
// Specify the name of the transcoded audio file.
$targetKey = "dest.aac";
// Construct the style string for audio processing and the audio transcoding parameters.
$animationStyle = "audio/convert,ss_10000,t_60000,f_aac,ab_96000";
// Construct an asynchronous processing instruction that includes the save path and the Base64-encoded bucket name and target file name.
$bucketNameEncoded = base64_encode($bucketName); // Base64-encode the bucket name.
$targetKeyEncoded = base64_encode($targetKey); // Base64-encode the target file name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $objectName, // The name of the object.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Audio merging
You can use the audio merging feature to combine multiple audio files into a single audio file and convert it to the required format.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define global variables.
$region = ''; // The storage region.
$bucketName = ''; // The name of the bucket.
// Parse the command line arguments.
$options = getopt('', ['region:', 'bucket:']);
// Check whether the storage region is empty.
if (empty($options['region'])) {
echo "Error: invalid parameters, region required\n";
exit(1);
}
// Check whether the bucket name is empty.
if (empty($options['bucket'])) {
echo "Error: invalid parameters, bucket name required\n";
exit(1);
}
// Obtain the values of the command line arguments.
$region = $options['region'];
$bucketName = $options['bucket'];
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// The names of the audio files to be merged.
$audio1 = "src1.mp3";
$audio2 = "src2.mp3";
// Specify the name of the merged audio file.
$targetAudio = "dest.aac";
// Construct the style string for audio processing and the audio merging parameters.
$audio1Encoded = base64_encode($audio1); // Base64-encode the name of the first audio file.
$audio2Encoded = base64_encode($audio2); // Base64-encode the name of the second audio file.
$style = sprintf(
"audio/concat,f_aac,ac_1,ar_44100,ab_96000,align_2/pre,o_%s/pre,o_%s,t_0",
$audio1Encoded,
$audio2Encoded
);
// Construct an asynchronous processing instruction that includes the save path and the Base64-encoded bucket name and target file name.
$bucketEncoded = base64_encode($bucketName); // Base64-encode the bucket name.
$targetEncoded = base64_encode($targetAudio); // Base64-encode the target file name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$style,
$bucketEncoded,
$targetEncoded
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Parse image blind watermark
The following sample code provides an example of how to parse a blind watermark in an image.
<?php
// Import the autoload file to load the dependency library.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This parameter specifies the region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This parameter specifies the domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The object name is required.
];
// Generate a list of long options to parse the 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 arguments 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 argument is missing.
exit(1);
}
}
// Obtain the values of the command line arguments.
$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.
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential 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);
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define global variables.
$region = ''; // The storage region.
$bucketName = ''; // The name of the bucket.
// Parse the command line arguments.
$options = getopt('', ['region:', 'bucket:']);
// Check whether the storage region is empty.
if (empty($options['region'])) {
echo "Error: invalid parameters, region required\n";
exit(1);
}
// Check whether the bucket name is empty.
if (empty($options['bucket'])) {
echo "Error: invalid parameters, bucket name required\n";
exit(1);
}
// Obtain the values of the command line arguments.
$region = $options['region'];
$bucketName = $options['bucket'];
// Use an environment variable to load the credentials, which are the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Specify the name of the watermarked image file.
$sourceKey = $objectName; // Specify the name of the image object to be processed.
// Specify the topic of the MNS message.
$topic = "imm-blindwatermark-test";
// Extract the watermark content from the specified image.
$style = "image/deblindwatermark,s_low,t_text";
$encodedTopic = rtrim(base64_encode($topic), '='); // Base64-encode and remove the trailing equal sign (=).
$process = sprintf(
"%s|sys/notify,topic_%s",
$style,
$encodedTopic
);
// Create a request object for asynchronous object processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucketName, // The name of the bucket.
key: $sourceKey, // Specify the name of the image to be processed.
asyncProcess: $process // The asynchronous processing instruction.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
References
For more information about the asynchronous processing feature, see Asynchronous processing.
For the sample code for the asynchronous processing feature, see AsyncProcessObject.