Spring Boot with Alibaba Cloud Object Storage Service

Overview

Prerequisites

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

Concepts

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

Dependencies

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>

Connect to OSS

  • Create
  • Disable
  • Delete access key
spring.application.name=spring-alibaba-ossserver.port=8080spring.cloud.alicloud.access-key=******fill***yours****here****
spring.cloud.alicloud.secret-key=******fill***yours****here****
spring.cloud.alicloud.oss.endpoint=oss-cn-hangzhou-internal.aliyuncs.com
management.endpoints.web.exposure.include=*

Use OSSClient to Save/Query Data

@Autowired
private OSS ossClient;
ossClient.createBucket("Bucket name goes here");
ossClient.doesBucketExist("Bucket name goes here");
ossClient.putObject("oss-test", "spring-alibaba-oss.json", this
.getClass().getClassLoader().getResourceAsStream("spring-alibaba-oss.json"));
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")
@GetMapping("/buckets/exist/{bucketName}")
public boolean bucketExists(@PathVariable String bucketName) {
return ossClient.doesBucketExist(bucketName);
}
@GetMapping("/objects/exist/{objectName}")
public boolean objectExists(@PathVariable String objectName) {
return ossClient.doesObjectExist(SpringAlibabaOssApplication.BUCKET_NAME, objectName);
}
@GetMapping("/upload")
public String upload() {
try {
ossClient.putObject("oss-test", "spring-alibaba-oss.json", this
.getClass().getClassLoader().getResourceAsStream("spring-alibaba-oss.json"));
} catch (Exception e) {
e.printStackTrace();
return "upload fail: " + e.getMessage();
}
return "upload success";
}
@GetMapping("/download")
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) {
e.printStackTrace();
return "download fail: " + e.getMessage();
}
}

Conclusion

Reference

--

--

--

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