Spring Boot with Alibaba Cloud Object Storage Service

By Dassi Orleando, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Overview

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.

Prerequisites

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

Concepts

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

  • 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

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

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

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.

  • 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

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

@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

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.

Reference

https://www.alibabacloud.com/blog/spring-boot-with-alibaba-cloud-object-storage-service_594905?spm=a2c41.13033977.0.0

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