Get started with OSS on CloudBox
OSS on CloudBox lets you monitor and process your local data. It is ideal for use cases that require low latency or unified management of multiple branch offices. This topic describes the basic operations of OSS on CloudBox, including how to create a bucket, upload an object, and download an object.
Prerequisites
-
OSS on CloudBox is available only in the China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu) regions.
-
You have purchased a CloudBox.
-
You have created a VPC and a vSwitch for the CloudBox.
-
You have contacted technical support to request a SingleTunnel network type for your CloudBox's VPC.
Step 1: Create a bucket
Use the OSS console
-
Log on to the OSS console.
-
In the left-side navigation pane, choose Data Service > OSS on CloudBox Buckets, and then click Create Bucket in the upper-left corner.
-
On the Create Bucket page, enter a bucket name, accept the default settings, and then click OK.
A bucket name must meet the following requirements:
-
The bucket name must be unique within the CloudBox.
- The name can contain only lowercase letters, digits, and hyphens (-).
- The name must start and end with a lowercase letter or a digit.
- The name must be 3 to 63 characters in length.
-
Use an Alibaba Cloud SDK
You can create an OSS on CloudBox bucket by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint for the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the OSS on CloudBox bucket, for example, examplebucket.
String bucketName = "examplebucket";
// Specify the region where the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the Cloud Box ID.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Create an OSSClient instance.
// Call shutdown() to release resources when the client is no longer needed.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
// Create a CreateBucketRequest object.
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
// Set the ACL of the OSS on CloudBox bucket to public-read. The default ACL is private.
//createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
// Create the OSS on CloudBox bucket.
ossClient.createBucket(createBucketRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request reached OSS but was rejected with an error response.");
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 a 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 alibabacloud_oss_v2 as oss
# Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# The region where the CloudBox is located. For example, cn-hangzhou.
cfg.region = "cn-hangzhou"
# The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
cfg.endpoint = "cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# The CloudBox ID.
cfg.cloud_box_id = "cb-xxxx"
client = oss.Client(cfg)
# Create a bucket. The bucket name must be globally unique.
result = client.put_bucket(oss.PutBucketRequest(
bucket="examplebucket",
acl="private",
))
print(f"status code: {result.status_code}, request id: {result.request_id}")package main
import (
"context"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
// The region where the CloudBox is located. For example, cn-hangzhou.
region := "cn-hangzhou"
// Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
// The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
WithEndpoint("cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com").
// The CloudBox ID.
WithCloudBoxId("cb-xxxx")
client := oss.NewClient(cfg)
// Create a bucket. The bucket name must be globally unique.
result, err := client.PutBucket(context.TODO(), &oss.PutBucketRequest{
Bucket: oss.Ptr("examplebucket"),
})
if err != nil {
log.Fatalf("failed to put bucket %v", err)
}
log.Printf("put bucket result:%#v\n", result)
}Use ossutil
To create an OSS on CloudBox bucket with ossutil, see put-bucket.
Use a REST API
If your application requires extensive customization, you can call the REST API directly. This requires you to write code to manually calculate the signature. For more information, see PutBucket.
Step 2: Upload an object
Use an Alibaba Cloud SDK
You can upload local files to an OSS on CloudBox bucket by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.File;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint of the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this 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 OSS on CloudBox bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the region where the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the ID of the CloudBox.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
String objectName = "exampledir/exampleobject.txt";
// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt.
// If you do not specify a local path, the file is uploaded from the local path of the project to which the sample program belongs.
String filePath= "D:\\localpath\\examplefile.txt";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
// Create a PutObjectRequest object.
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
// To set the storage class and access permissions during the upload, see 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();
}
}
}
}import alibabacloud_oss_v2 as oss
# Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# The region where the CloudBox is located. For example, cn-hangzhou.
cfg.region = "cn-hangzhou"
# The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
cfg.endpoint = "cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# The CloudBox ID.
cfg.cloud_box_id = "cb-xxxx"
client = oss.Client(cfg)
# Upload data to the specified object.
result = client.put_object(oss.PutObjectRequest(
bucket="examplebucket",
key="exampledir/exampleobject.txt",
body=b"Hello OSS",
))
print(f"status code: {result.status_code}, etag: {result.etag}")package main
import (
"context"
"log"
"strings"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
// The region where the CloudBox is located. For example, cn-hangzhou.
region := "cn-hangzhou"
// Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
// The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
WithEndpoint("cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com").
// The CloudBox ID.
WithCloudBoxId("cb-xxxx")
client := oss.NewClient(cfg)
// Upload data to the specified object.
result, err := client.PutObject(context.TODO(), &oss.PutObjectRequest{
Bucket: oss.Ptr("examplebucket"),
Key: oss.Ptr("exampledir/exampleobject.txt"),
Body: strings.NewReader("Hello OSS"),
})
if err != nil {
log.Fatalf("failed to put object %v", err)
}
log.Printf("put object result:%#v\n", result)
}Use ossutil
For simple uploads using ossutil, see cp (upload files).
Use a REST API
If your application requires extensive customization, you can call the REST API directly. This requires you to write code to manually calculate the signature. For more information, see PutObject.
Step 3: Download an object
Use an Alibaba Cloud SDK
You can download objects by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support. The following code examples use the Java, Python, and Go SDKs.
package com.aliyun.oss.demo;
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;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint of the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this 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 OSS on CloudBox bucket. For example, examplebucket.
String bucketName = "examplebucket";
// Specify the region where the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the ID of the CloudBox.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Specify the full path of the object, excluding the bucket name. For example, exampledir/exampleobject.txt.
String objectName = "exampledir/exampleobject.txt";
// Specify the local file path to save the downloaded object.
String pathName = "D:\\localpath\\examplefile.txt";
// Create an OSSClient instance using Signature Version 4 (V4).
// Call shutdown() to release resources when the client is no longer needed.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
// Download the object to the specified local file.
// If the file exists, it is overwritten. If it does not exist, a new file is created.
// If you do not specify a local path, the file is saved to the path of the project to which the sample program belongs.
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
} catch (OSSException oe) {
System.out.println("OSS rejected the request.");
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("Client failed to communicate with OSS.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}import alibabacloud_oss_v2 as oss
# Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# The region where the CloudBox is located. For example, cn-hangzhou.
cfg.region = "cn-hangzhou"
# The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
cfg.endpoint = "cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# The CloudBox ID.
cfg.cloud_box_id = "cb-xxxx"
client = oss.Client(cfg)
# Download the object to a local file.
result = client.get_object_to_file(oss.GetObjectRequest(
bucket="examplebucket",
key="exampledir/exampleobject.txt",
), "download.txt")
print(f"status code: {result.status_code}, content length: {result.content_length}")package main
import (
"context"
"io"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
// The region where the CloudBox is located. For example, cn-hangzhou.
region := "cn-hangzhou"
// Obtain access credentials from environment variables. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables before you run the code.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
// The CloudBox data domain name, in the format CloudboxId.Region.oss-cloudbox.aliyuncs.com.
WithEndpoint("cb-xxxx.cn-hangzhou.oss-cloudbox.aliyuncs.com").
// The CloudBox ID.
WithCloudBoxId("cb-xxxx")
client := oss.NewClient(cfg)
// Download the specified object.
result, err := client.GetObject(context.TODO(), &oss.GetObjectRequest{
Bucket: oss.Ptr("examplebucket"),
Key: oss.Ptr("exampledir/exampleobject.txt"),
})
if err != nil {
log.Fatalf("failed to get object %v", err)
}
defer result.Body.Close()
data, _ := io.ReadAll(result.Body)
log.Printf("get object content length:%d\n", len(data))
}Use ossutil
For simple downloads using ossutil, see cp (download files).
Use a REST API
If your application requires extensive customization, you can call the REST API directly. This requires you to write code to manually calculate the signature. For more information, see GetObject.