Spring Boot with Alibaba Cloud Object Storage Service



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


  • 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.



Connect to OSS

  • Create
  • Disable
  • Delete access key

Use OSSClient to Save/Query Data

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();






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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Upcoming change on FarmHero BSC

Shaping software development teams

Connecting the ESP32-DevKitC and AWS IoT Using Mongoose OS, Part II

My Alibaba Cloud ACE Experience: Part 2

How Kubernetes is helping Bose to solve its challenges

Functional testing Django applications using Pytest Part 2(Selenium testing)

Hacking a computer using Arduino

TryHackMe: Classic Passwd.

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

More from Medium

The proxy pattern in Microservices

[Spring Boot] Testing Apache Camel SEDA Endpoint

Apache Kafka Configuration In Spring Boot With Producer And Consumer Example

Circuit Breaker Pattern With Netflix-Hystrix: Java