Com o processamento assíncrono, um programa pode continuar executando outras tarefas sem aguardar a conclusão da tarefa atual. Use o parâmetro x-oss-async-process para processar dados de forma assíncrona no Object Storage Service (OSS).
Observação de uso
Os códigos de exemplo deste tópico usam o ID da região China (Hangzhou) (
cn-hangzhou) e o endpoint público. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use o endpoint interno. Para obter informações sobre o mapeamento entre regiões e endpoints do OSS, consulte Regions and endpoints.
Código de exemplo
O código de exemplo a seguir converte um documento para um formato específico:
<?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.
);
Cenários comuns
Transcodificação de vídeo
A transcodificação de vídeo altera o formato de codificação de um vídeo, reduz o tamanho do arquivo ao ajustar parâmetros como resolução e taxa de bits, e converte o formato do contêiner.
<?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.
);
Conversão de vídeo em imagem animada
A conversão de vídeo em imagem animada transforma um vídeo em imagens animadas no formato GIF ou WebP.
<?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.
);
Conversão de vídeo em sprite
A conversão de vídeo em sprite extrai quadros de vídeo e gera sprites com base em regras específicas.
<?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.
);
Captura de quadros de vídeo
A captura de quadros de vídeo extrai quadros com base em regras específicas e os converte para um formato determinado.
<?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.
);
Mesclagem de vídeos
A mesclagem de vídeos combina vários vídeos em um único arquivo de vídeo em um formato específico.
<?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.
);
Transcodificação de áudio
A transcodificação de áudio converte arquivos de áudio para um formato específico.
<?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.
);
Mesclagem de áudios
A mesclagem de áudios combina vários arquivos de áudio em um único arquivo em um formato específico.
<?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.
);
Decodificação de marca d'água invisível
O código de exemplo a seguir decodifica uma marca d'água invisível de uma imagem:
<?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.
);
Referências
Para obter mais informações sobre o processamento assíncrono, consulte Asynchronous processing.
Para obter informações sobre o código de exemplo de processamento assíncrono, consulte AsyncProcessObject.