If you want to accelerate long-distance data transfers for a bucket, such as accelerating uploads or downloads of files from the Chinese mainland to a bucket outside the Chinese mainland, you can enable transfer acceleration. The transfer acceleration feature provides an optimized end-to-end solution with data centers distributed around the world to accelerate access to Object Storage Service (OSS) over the Internet. When the feature is enabled, requests destined for your bucket are routed to the data center nearest to users over the optimal network path and protocol.
Prerequisites
Real-name registration for the current Alibaba Cloud account is completed. For more information, see how to perform real-name registration?
Use scenario
Accelerate remote data transfers
For example, global forums and online collaboration platforms. Some customers may have a poor upload or download experience because of long transmission distances. Transfer acceleration allows users from different regions to access data over the optimal network path. This accelerates data transfer and improves user experience.
Accelerate uploads and downloads of gigabyte-grade and terabyte-grade objects
When large objects are uploaded or downloaded over long geographical distances, transmission failures may occur due to high network latencies. Transfer acceleration combines optimal route selection, protocol stack tuning, and transmission algorithm optimization to reduce timeouts during remote transfers of large objects over the Internet. You can also use transfer acceleration in combination with multipart upload and resumable download to create a solution for uploading and downloading large objects over long distances.
Accelerate the download of dynamic and cold data
User experience is a driving factor for product competitiveness and customer retention in applications that require high data download speeds, such as photo management applications, games, e-commerce applications, enterprise portal websites, and financial applications. You can use transfer acceleration to maximize bandwidth utilization and accelerate data transfers from OSS.
Precautions
When you use an acceleration endpoint to upload data, you are charged transfer acceleration fees based on the traffic that is generated. When you use an acceleration endpoint to download data, you are charged transfer acceleration fees based on the traffic that is generated and the fees for outbound traffic over the Internet. For example, if you use an acceleration endpoint to download 1 GB data from a bucket for which transfer acceleration is enabled, you are charged transfer acceleration fees of 1 GB data and the fees for 1 GB outbound traffic over the Internet. For more information, see transfer acceleration fees and traffic fees.
When transfer acceleration is enabled, an acceleration endpoint is required to improve the access speed. However, you can still use the default endpoint to access OSS. In scenarios where transfer acceleration is not required, you can use the default endpoint to prevent unnecessary charges. For more information, see OSS regions and endpoints.
Acceleration endpoints can be accessed only by calling API operations that use the HTTP or HTTPS protocol. Access by calling API operations that use other protocols, such as RTMP, is not supported.
Based on the transfer acceleration logic, HTTPS may be used to ensure secure data transfers. Even if the client uses an acceleration endpoint to access a bucket over HTTP, the protocol recorded in the access log may be HTTPS.
Enable transfer acceleration
After enabling transfer acceleration, the bucket will have the following two transfer acceleration endpoints in addition to the default endpoint.
Global Accelerator Endpoint: The address is
oss-accelerate.aliyuncs.com
. Transfer acceleration access points are distributed across the world. You can use this domain name to accelerate data transfer for buckets in all regions.Non-mainland China acceleration endpoint: The address is
oss-accelerate-overseas.aliyuncs.com
. Transfer acceleration access points are distributed across regions outside the Chinese mainland. You can use the acceleration endpoint to map a custom domain name without an ICP filing to a bucket in the China (Hong Kong) region or in a region outside the Chinese mainland.
You can use one of the following methods to enable transfer acceleration.
Use the OSS console
Log on to the OSS console.
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
In the left navigation bar, select Bucket SettingsTransfer Acceleration.
In the Transfer Acceleration page, turn on the Enable Transfer Acceleration switch, then in the dialog box, click OK.
After you enable transfer acceleration, approximately 30 minutes are required for the change to take effect. Disabling transfer acceleration also requires approximately 30 minutes for the change to take effect.
Use Alibaba Cloud SDK
The following sample code provides examples on how to enable transfer acceleration by using OSS SDKs for common programming languages. For more information, see the referenced document.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Configure transfer acceleration for the bucket.
// If enabled is set to true, transfer acceleration is enabled. If enabled is set to false, transfer acceleration is disabled.
boolean enabled = true;
ossClient.setBucketTransferAcceleration(bucketName, enabled);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(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 = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Configure transfer acceleration for the bucket.
# If enabled is set to true, transfer acceleration is enabled. If enabled is set to false, transfer acceleration is disabled.
enabled = 'true'
bucket.put_bucket_transfer_acceleration(enabled)
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // Region in which the bucket is located.
bucketName string // Name of the bucket.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to enable transfer acceleration for the bucket.
request := &oss.PutBucketTransferAccelerationRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
TransferAccelerationConfiguration: &oss.TransferAccelerationConfiguration{
Enabled: oss.Ptr(true), // Enable transfer acceleration.
},
}
// Execute the request to enable transfer acceleration.
result, err := client.PutBucketTransferAcceleration(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put bucket transfer acceleration %v", err)
}
// Display the result.
log.Printf("put bucket transfer acceleration result:%#v\n", result)
}
Use the command line tool ossutil
You can use the command line tool ossutil to enable transfer acceleration. For the installation of ossutil, see install ossutil.
The following command is used to configure transfer acceleration for the bucket examplebucket.
ossutil api put-bucket-transfer-acceleration --bucket examplebucket --transfer-acceleration-configuration "{\"Enabled\":\"true\"}"
For more information about this command, see put-bucket-transfer-acceleration.
Use transfer acceleration
Use a browser
When accessing OSS through a browser, replace the Endpoint field of the file URL with the transfer acceleration Endpoint. For example, https://test.oss-cn-shenzhen.aliyuncs.com/myphoto.jpg
should be changed to https://test.oss-accelerate.aliyuncs.com/myphoto.jpg
. If the access control list (ACL) of the object you want to access is private, you must sign the access request.
Use the command line tool ossutil
When you persistently enable the transfer acceleration feature to access OSS through ossutil, you can replace the Endpoint in the configuration file with the transfer acceleration Endpoint.
First, locate and open the configuration file for ossutil. By default, the configuration file is located at the following path:
Linux:
/home/user/.ossutilconfig
Windows:
C:\Users\user\.ossutilconfig
macOS:
/Users/user/.ossutilconfig
If you specify a different path when configuring ossutil, use the path you specified.
Modify the Endpoint. In the configuration file, locate
endpoint
field and modify its value to transfer acceleration Endpoint:[Credentials] endpoint = oss-accelerate.aliyuncs.com accessKeyID = LTAI4Fw2NbDUCV8z******** accessKeySecret = 67DLVBkH7EamOjy2W5RVAH******** region=cn-hangzhou
Save the changes to the configuration file.
When using commands related to ossutil, you can also add
-e oss-accelerate.aliyuncs.com
in the command example, which is only effective for the current command. The following command indicates that the transfer acceleration endpoint is used in the upload scenario of the cp command.ossutil cp /path/to/local/file oss://examplebucket/exampleobject -e oss-accelerate.aliyuncs.com
Use the graphical management tool ossbrowser 1.0
When you access OSS by using ossbrowser, you must specify your AccessKey (AK) information and the preset OSS path.
The following table describes the parameters as follows:
Parameter | Note |
Endpoint | Select Custom, and fill in the transfer acceleration Endpoint |
AccessKeyId, AccessKeySecret | Enter the AccessKey pair of your account. For information on how to obtain the AccessKey, see Create AccessKey. Important To ensure data security, we recommend that you use the AccessKey pair of a RAM user to log on to ossbrowser. Before logging in with a RAM user, you need to grant the RAM user the |
Preset OSS Path | Specify the access permissions on a bucket or the resources stored in the bucket. The default OSS path format is |
Configuration examples are as follows:
Use Alibaba Cloud SDK
When accessing OSS through SDKs in various languages, you can set the Endpoint to the transfer acceleration Endpoint. This section uses simple upload and simple download as examples for illustration.
Upload using simple method
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.PutObjectRequest; import java.io.File; public class Demo { public static void main(String[] args) throws Exception { // Specify an acceleration endpoint. In this example, the global acceleration endpoint is used. String endpoint = "https://oss-accelerate.aliyuncs.com"; // We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked, which compromises the security of all resources in your account. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the name of the bucket. Example: examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. String objectName = "exampledir/exampleobject.txt"; // Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. // If you do not specify the path of the local file, the file is uploaded from the local path of the project to which the sample program belongs. String filePath= "D:\\localpath\\examplefile.txt"; // Specify the region of the bucket. In this example, the region is set to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); // Explicitly specify the use of the V4 signature clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Create a PutObject request. PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, filePath); // Optional. Specify the storage class and access control list (ACL) of the object by using the following sample code. // ObjectMetadata metadata = new ObjectMetadata(); // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString()); // metadata.setObjectAcl(CannedAccessControlList.Private); // putObjectRequest.setMetadata(metadata); // Upload the file. ossClient.putObject(putObjectRequest); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
package main import ( "context" "flag" "log" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" ) // Define the global variables var ( region string // The region in which the bucket is located. bucketName string // The name of the bucket. objectName string // The name of the object. ) // Use the init function to initialize command line parameters func init() { flag.StringVar(®ion, "region", "", "The region in which the bucket is located.") flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.") flag.StringVar(&objectName, "object", "", "The name of the object.") } func main() { // Parse command line parameters flag.Parse() // Check whether the bucket name is empty if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Check whether the object name is empty if len(objectName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, object name required") } // Load the default configurations and specify the credential provider and region cfg := oss.LoadDefaultConfig(). WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()). WithRegion(region). WithEndpoint("oss-accelerate.aliyuncs.com") // Set the acceleration endpoint // Create an OSS client client := oss.NewClient(cfg) // Specify the path of the local file that you want to upload. Example: /Users/localpath/exampleobject.txt localFile := "/Users/localpath/exampleobject.txt" // Create a request to upload an object putRequest := &oss.PutObjectRequest{ Bucket: oss.Ptr(bucketName), // The name of the bucket. Key: oss.Ptr(objectName), // The name of the object. StorageClass: oss.StorageClassStandard, // Set the storage class of the object to Standard. Acl: oss.ObjectACLPrivate, // Set the access control list (ACL) of the object to private. } // Execute the request to upload an object result, err := client.PutObjectFromFile(context.TODO(), putRequest, localFile) if err != nil { log.Fatalf("failed to put object from file %v", err) } // Display the result of the object upload log.Printf("put object result:%#v\n", result) }
Simple download
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.GetObjectRequest; import java.io.File; public class Demo { public static void main(String[] args) throws Exception { // Specify an acceleration endpoint. In this example, the global acceleration endpoint is used. String endpoint = "https://oss-accelerate.aliyuncs.com"; // We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked, which compromises the security of all resources in your account. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the name of the bucket. Example: examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt. String objectName = "testfolder/exampleobject.txt"; String filePath = "D:\\localpath\\examplefile.txt"; // Specify the region of the bucket. In this example, the region is set to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); // Explicitly specify the use of the V4 signature clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Download the object to the specified path on your computer. If a file with the same name already exists, the downloaded object overwrites the file. If no file with the same name exists, a file is created. // If you do not specify the path of the local file, the downloaded object is saved to the path of the project to which the sample program belongs. ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(filePath)); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
package main import ( "context" "flag" "log" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" ) // Define the global variables var ( region string // The region in which the bucket is located. bucketName string // The name of the bucket. objectName string // The name of the object. ) // Use the init function to initialize command line parameters func init() { flag.StringVar(®ion, "region", "", "The region in which the bucket is located.") flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.") flag.StringVar(&objectName, "object", "", "The name of the object.") } func main() { // Parse command line parameters flag.Parse() // Check whether the bucket name is empty if len(bucketName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, bucket name required") } // Check whether the region is empty if len(region) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, region required") } // Check whether the object name is empty if len(objectName) == 0 { flag.PrintDefaults() log.Fatalf("invalid parameters, object name required") } // Load the default configurations and specify the credential provider and region cfg := oss.LoadDefaultConfig(). WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()). WithRegion(region). WithEndpoint("oss-accelerate.aliyuncs.com") // Set the acceleration endpoint // Create an OSS client client := oss.NewClient(cfg) // Specify the path of the local file that you want to download. Example: /Users/localpath/downloadobject.txt localFile := "/Users/localpath/exampleobject.txt" // Create a request to download the object getRequest := &oss.GetObjectRequest{ Bucket: oss.Ptr(bucketName), // The name of the bucket. Key: oss.Ptr(objectName), // The name of the object. } // Download the object to a local device and process the result result, err := client.GetObjectToFile(context.TODO(), getRequest, localFile) if err != nil { log.Fatalf("failed to get object to file %v", err) } log.Printf("Get object sucessfully, Result: HTTPStatusCode=%d, ContentLength=%d\n", result.StatusCode, result.ContentLength) }
Test the effect of transfer acceleration
Use the command line tool ossutil
Test the difference in time required to upload files in the ossutil command example before transfer acceleration is enabled (that is, using -e oss-us-west-1.aliyuncs.com
) and after transfer acceleration is enabled (that is, using -e oss-accelerate.aliyuncs.com
).
FAQ
Besides transfer acceleration, does OSS provide other acceleration solutions?
Is it supported to configure multiple acceleration solutions simultaneously?
Why can't the transfer acceleration endpoint list buckets?
Related API operations
The above operation method is implemented based on the underlying API. If your program requires a high level of customization, you can directly call a RESTful API. To directly call a RESTful API, you must include the signature calculation in your code. For more information, see PutBucketTransferAcceleration.