Integrating Alibaba Cloud OSS with Spring Cloud for Server‑Side Signed Direct File Upload
This article explains why traditional server‑mediated file uploads are inefficient, introduces the server‑side signed direct upload approach for Alibaba Cloud OSS, and provides a step‑by‑step guide—including OSS setup, Maven dependencies, Spring Cloud configuration, policy‑generation controller, gateway routing, CORS settings, and a Vue upload component—complete with code examples.
File upload is a common requirement in distributed systems, and uploading files to a local server becomes a bottleneck in terms of speed, scalability, and cost. Using a mature cloud storage service such as Alibaba Cloud OSS solves these problems.
The article first describes three drawbacks of the traditional upload flow (slow, poor scalability, high cost) and then introduces the server‑side signed direct upload solution, which lets the client upload files directly to OSS after obtaining a signed policy from the backend.
Technical implementation :
Open an OSS account, create a bucket, and obtain AccessKeyId and AccessKeySecret.
Add the OSS SDK to a Maven project: <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.8.0</version> </dependency>
Integrate spring-cloud-starter-alicloud-oss in the passjava-common module and configure the access keys, endpoint, and bucket in application.yml . spring: cloud: alicloud: access-key: xxxx secret-key: xxxx oss: endpoint: oss-cn-beijing.aliyuncs.com
Create a third‑party service ( passjava-thirdparty ) that exposes a /getPolicy endpoint. The controller builds the upload policy, signs it, and returns the required fields (policy, signature, accessid, dir, host, expire). @RestController @RequestMapping("/thirdparty/v1/admin/oss") public class OssController { @Autowired OSS ossClient; @Value("${spring.cloud.alicloud.access-key}") private String accessId; // ... other fields ... @RequestMapping("/getPolicy") public Map getPolicy() { /* generate policy and signature */ } }
Enable service discovery with @EnableDiscoveryClient and configure Nacos for service registration and configuration.
Configure Spring Cloud Gateway to route /api/thirdparty/** requests to the third‑party service. spring: cloud: gateway: routes: - id: route_thirdparty uri: lb://passjava-thirdparty predicates: - Path=/api/thirdparty/** filters: - RewritePath=/api/(? .*),/${segment}
Set up CORS to allow all POST requests.
Implement a Vue single‑file upload component that obtains the policy via an API call, fills the dataObj fields, and uploads directly to OSS. <el-upload action="http://passjava.oss-cn-beijing.aliyuncs.com" :data="dataObj" ...> ... </el-upload>
Finally, the article provides testing URLs for the policy API and demonstrates successful file upload, confirming that the integration works end‑to‑end.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.