With asynchronous processing, a program can continue running other tasks without waiting for the current task to complete. Use the x-oss-async-process parameter to asynchronously process data in Object Storage Service (OSS).
Usage note
-
In the sample code of this topic, the ID of the China (Hangzhou) region (
cn-hangzhou) and the public endpoint are used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For information about the mappings between OSS regions and endpoints, see Regions and Endpoints.
Sample code
The following sample code converts a document to a specific format:
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the style used to process the video and convert it 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 contains the Base64-encoded bucket and object names.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s",
$style,
rtrim(base64_encode($bucket), '='), // The Base64-encoded bucket name. Remove the equal sign (=) at the end.
rtrim(base64_encode($key), '=') // The Base64-encoded object name. Remove the equal sign (=) at the end.
);
// Create a request object for asynchronous processing.
$request = new Oss\Models\AsyncProcessObjectRequest(
bucket: $bucket, // The name of the bucket.
key: $key // The name of the object.
);
// Specify the processing instruction.
$request->process = $process;
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Common scenarios
Video transcoding
Video transcoding changes the encoding format of a video, reduces the file size by adjusting parameters such as the resolution and bit rate, and converts the container format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the transcoded video.
$targetObject = "dest.avi";
// Specify a processing style to specify the format, video codec, resolution, bit rate, frame rate, audio codec, and audio bit rate.
$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 contains the processing style and the Base64-encoded bucket and object names.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s",
$style,
rtrim(base64_encode($bucketName), '='), // The Base64-encoded bucket name. Remove the equal sign (=) at the end.
rtrim(base64_encode($targetObject), '=') // The Base64-encoded object name. Remove the equal sign (=) at the end.
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video-to-animated-image conversion
Video-to-animated-image conversion converts a video to animated images in the GIF or WebP format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
exit(1);
}
}
// Obtain the values of the command line parameters
$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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the animated GIF image.
$targetKey = "destexample.gif";
// Configure parameters for converting the video to a GIF image, including parameters used to specify the width, height, and frame interval.
$animationStyle = "video/animation,f_gif,w_100,h_100,inter_1000";
// Construct an asynchronous processing instruction that contains the storage path and the Base64-encoded bucket and object names.
$bucketNameEncoded = base64_encode($bucketName); // The Base64-encoded bucket name.
$targetKeyEncoded = base64_encode($targetKey); // The Base64-encoded object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video-to-sprite conversion
Video-to-sprite conversion extracts video frames and generates sprites based on specific rules.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the output sprite.
$targetKey = "example.jpg";
// Configure parameters for converting the video to a sprite, including parameters used to specify 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 contains the storage path and the Base64-encoded bucket and object names.
$bucketNameEncoded = base64_encode($bucketName); // The Base64-encoded bucket name.
$targetKeyEncoded = base64_encode($targetKey); // The Base64-encoded object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video frame capture
Video frame capture extracts video frames based on specific rules and converts them to a specific format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the captured frame.
$targetKey = "dest.png";
// Configure parameters for capturing video frames, including parameters used to specify the format, width, height, crop method, frame interval.
$animationStyle = "video/snapshots,f_jpg,w_100,h_100,scaletype_crop,inter_10000";
// Construct an asynchronous processing instruction that contains the storage path and the Base64-encoded bucket and object names.
$bucketNameEncoded = base64_encode($bucketName); // The Base64-encoded bucket name.
$targetKeyEncoded = base64_encode($targetKey); // The Base64-encoded object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Video merging
Video merging combines multiple videos into a single video file in a specific format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the merged video.
$targetObject = "dest.mp4";
// Specify the name of the video to be merged.
$video1 = "concat1.mp4";
$video2 = "concat2.mp4";
// Create a style string and configure parameters for video merging.
$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), '='), // The Base64-encoded name of the first video. Remove the equal sign (=) at the end.
rtrim(base64_encode($video2), '=') // The Base64-encoded name of the second video. Remove the equal sign (=) at the end.
);
// Constrcut an asynchronous processing instruction.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$style,
rtrim(base64_encode($bucketName), '='), // The Base64-encoded bucket name. Remove the equal sign (=) at the end.
rtrim(base64_encode($targetObject), '=') // The Base64-encoded object name. Remove the equal sign (=) at the end.
);
// Call the asyncProcessObject method to asynchronously process the object.
$result = $client->asyncProcessObject($request);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Audio transcoding
Audio transcoding converts audio files to a specific format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the require arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the transcoded audio file.
$targetKey = "dest.aac";
// Create a style string and configure parameters for audio transcoding.
$animationStyle = "audio/convert,ss_10000,t_60000,f_aac,ab_96000";
// Construct an asynchronous processing instruction that contains the storage path and the Base64-encoded bucket and object names.
$bucketNameEncoded = base64_encode($bucketName); // The Base64-encoded bucket name.
$targetKeyEncoded = base64_encode($targetKey); // The Base64-encoded object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$animationStyle,
$bucketNameEncoded,
$targetKeyEncoded
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Audio merging
Audio merging combines multiple audio files into a single audio file in a specific format.
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the global variables.
$region = ''; // The region where the bucket is located.
$bucketName = ''; // The name of the bucket.
// Parse command line arguments.
$options = getopt('', ['region:', 'bucket:']);
// Check whether the region is specified.
if (empty($options['region'])) {
echo "Error: invalid parameters, region required\n";
exit(1);
}
// Check whether the bucket name is specified.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Create an OSSClient 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";
// Create a style string and configure parameters for audio merging.
$audio1Encoded = base64_encode($audio1); // The Base64-encoded name of the first audio file.
$audio2Encoded = base64_encode($audio2); // The Base64-encoded 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 contains the storage path and the Base64-encoded bucket and object names.
$bucketEncoded = base64_encode($bucketName); // The Base64-encoded bucket name.
$targetEncoded = base64_encode($targetAudio); // The Base64-encoded object name.
$process = sprintf(
"%s|sys/saveas,b_%s,o_%s/notify,topic_QXVkaW9Db252ZXJ0",
$style,
$bucketEncoded,
$targetEncoded
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
Blind watermark decoding
The following sample code decodes a blind watermark from an image:
<?php
// Import the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify the descriptions of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Generate a long options list to parse the command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each argument to specify that a value is required.
}, array_keys($optsdesc));
// Parse command line arguments.
$options = getopt("", $longopts);
// Check whether the required arguments are configured.
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 that the required arguments are not configured.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Specify the endpoint if provided.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the global variables.
$region = ''; // The region where the bucket is located.
$bucketName = ''; // The name of the bucket.
// Parse the command line arguments.
$options = getopt('', ['region:', 'bucket:']);
// Check whether the region is specified.
if (empty($options['region'])) {
echo "Error: invalid parameters, region required\n";
exit(1);
}
// Check whether the bucket name is specified.
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 environment variables to load the AccessKey ID and AccessKey secret.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the name of the watermarked image.
$sourceKey = $objectName; // Specify the name of the image object to be processed.
// Specify the Simple Message Queue (SMQ, formerly MNS) topic.
$topic = "imm-blindwatermark-test";
// Extract the watermark from the specified image.
$style = "image/deblindwatermark,s_low,t_text";
$encodedTopic = rtrim(base64_encode($topic), '='); // The Base64-encoded topic name. Remove the equal sign (=) at the end.
$process = sprintf(
"%s|sys/notify,topic_%s",
$style,
$encodedTopic
);
// Create a request object for asynchronous 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);
// Display the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code.
'request id:' . $result->requestId . PHP_EOL . // The ID of the request.
'async process result:' . var_export($result, true) . PHP_EOL // The result of asynchronous processing.
);
References
-
For more information about asynchronous processing, see Asynchronous processing.
-
For information about the sample code of asynchronous processing, see AsyncProcessObject.