Todos os produtos
Search
Central de documentação

Object Storage Service:Aceleração de transferência (PHP SDK V2)

Última atualização: Sep 22, 2026

A aceleração de transferência melhora a velocidade de acesso ao OSS para usuários em todo o mundo. Este recurso é ideal para cenários como transmissão de dados de longa distância e upload ou download de arquivos grandes, com tamanho na casa dos gigabytes ou terabytes.

Precauções

  • O código de exemplo deste tópico usa a região China (Hangzhou), com o ID de região cn-hangzhou. Por padrão, o OSS usa o endpoint público. Se você precisar acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais informações sobre as regiões e os endpoints que o OSS suporta, consulte Regions and endpoints.

Código de exemplo

Ative a aceleração de transferência

O código a seguir mostra como ativar o recurso de aceleração de transferência para um bucket:

<?php

// Import the autoloader file to load dependencies.
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. 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. The domain name that can be used by other services to access OSS.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
];

// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon is added after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Check whether required parameters are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required parameter is missing.
        exit(1);
    }
}

// Obtain the values of command line arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.

// Load credentials, including the AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a request object to enable transfer acceleration for the bucket.
$request = new Oss\Models\PutBucketTransferAccelerationRequest(
    bucket: $bucket, // The bucket name.
    transferAccelerationConfiguration: new Oss\Models\TransferAccelerationConfiguration(
        enabled: true // Enable transfer acceleration.
    )
);

// Call the putBucketTransferAcceleration method to enable transfer acceleration for the bucket.
$result = $client->putBucketTransferAcceleration($request);

// Print the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
    'request id:' . $result->requestId . PHP_EOL     // The unique ID of the request.
);

Consulte o status de aceleração de transferência

O código a seguir mostra como consultar o status de aceleração de transferência de um bucket:

<?php

// Import the autoloader file to load dependencies.
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. 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. The domain name that can be used by other services to access OSS.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
];

// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon is added after each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Check whether required parameters are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required parameter is missing.
        exit(1);
    }
}

// Obtain the values of command line arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.

// Load credentials, including the AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a request object to query the transfer acceleration status of the bucket.
$request = new Oss\Models\GetBucketTransferAccelerationRequest(
    bucket: $bucket // The bucket name.
);

// Call the getBucketTransferAcceleration method to query the transfer acceleration status of the bucket.
$result = $client->getBucketTransferAcceleration($request);

// Print the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
    'request id:' . $result->requestId . PHP_EOL .   // The unique ID of the request.
    'transfer acceleration status:' . var_export($result->transferAccelerationConfiguration->enabled, true) . PHP_EOL // The status that indicates whether transfer acceleration is enabled.
);

Referências