Spring Boot with Alibaba Cloud Object Storage Service


Alibaba Cloud Object Storage Service (OSS) is an encrypted storage service that allows you to safely save, back up, and archive any volume of data in the cloud. OSS is cost-effective, highly secure while being a very reliable solution for any use. It’s possible to interact with it using a restful API.


  1. Basic knowledge of Spring Boot
  2. An Alibaba Cloud account


Let’s grab some important concepts around the OSS service:

  • Storage Class: OSS provides three storage classes: Standard, Infrequent Access, and Archive. These storage classes cover various data storage scenarios from hot data to cold data. For more information, see Introduction to storage classes.
  • Bucket: A bucket is a container for objects stored in OSS. Every object is contained in a bucket.
  • Objects: which are files, are the fundamental entities stored in OSS. An object is defined by some metadata, data and key. The key is a unique object field present in a bucket.
  • Region: represents the physical location of an OSS data center. You can choose the region that has the least latency while being affordable for you.
  • Endpoint: it’s nothing more than the domain name used to access the OSS. OSS provides external services through HTTP RESTful APIs. Different regions use different endpoints. Let’s note that each region has its endpoint.
  • AccessKey: An AccessKey (AK) is composed of an AccessKeyId and an AccessKeySecret.


The following Maven dependency is used to make the whole logic possible, it’s the spring wrapper for the SDK interface:


Connect to OSS

To perform any operation on our OSS we first need to connect ourselves giving us the required permissions to perform authenticated actions from our application.

  • Create
  • Disable
  • Delete access key

Use OSSClient to Save/Query Data

Now that the whole thing is well setting up, we can perform some operations directly under our account to use Alibaba Cloud OSS service.

private OSS ossClient;
ossClient.createBucket("Bucket name goes here");
ossClient.doesBucketExist("Bucket name goes here");
ossClient.putObject("oss-test", "spring-alibaba-oss.json", this
OSSObject ossObject = ossClient.getObject("oss-test", "spring-alibaba-oss.json");
ossClient.deleteObject("oss-test", "spring-alibaba-oss.json");
ossClient.doesObjectExist("oss-test", "spring-alibaba-oss.json")
public boolean bucketExists(@PathVariable String bucketName) {
return ossClient.doesBucketExist(bucketName);
public boolean objectExists(@PathVariable String objectName) {
return ossClient.doesObjectExist(SpringAlibabaOssApplication.BUCKET_NAME, objectName);
public String upload() {
try {
ossClient.putObject("oss-test", "spring-alibaba-oss.json", this
} catch (Exception e) {
return "upload fail: " + e.getMessage();
return "upload success";
public String download() {
try {
OSSObject ossObject = ossClient.getObject(SpringAlibabaOssApplication.BUCKET_NAME, "spring-alibaba-oss.json");
return "download success, content: " + IOUtils
.readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8);
} catch (Exception e) {
return "download fail: " + e.getMessage();


In this article, we’ve seen how to use the Alibaba Cloud OSS service within a Spring-Boot project plus doing some operations on our buckets/objects.





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com