All Products
Search
Document Center

Object Storage Service:Enable transfer acceleration by using OSS SDK for PHP

Last Updated:Mar 11, 2024

The transfer acceleration feature allows users worldwide to access objects stored in Object Storage Service (OSS) more quickly. This feature is applicable to scenarios in which data must be transferred over long geographical distances. This feature can also be used to download or upload large objects that are gigabytes or terabytes in size.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

Enable transfer acceleration for a bucket

The following sample code provides an example on how to enable transfer acceleration for a bucket named examplebucket:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
try{
    $ossClient->putBucketTransferAcceleration($bucket,true);
    printf('putBucketTransferAcceleration SUCCESS' . "\n");
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}            

For information about the API operation that you can call to enable transfer acceleration for a bucket, see PutBucketTransferAcceleration.

Query the transfer acceleration status of a bucket

The following sample code provides an example on how to query the transfer acceleration status of a bucket named examplebucket:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
try{
    // Query the transfer acceleration status of the bucket. 
    // If the return value is true, transfer acceleration is enabled for the bucket. If the return value is false, transfer acceleration is disabled for the bucket. 
    $result = $ossClient->getBucketTransferAcceleration($bucket);
    if($result === true){
        $status = 'enabled';
    }else{
        $status = 'disabled';
    }
    printf('getBucketTransferAcceleration Status:'.$status."\n");
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

For information about the API operation that you can call to query the transfer acceleration status of a bucket, see GetBucketTransferAcceleration.