springboot uploads pictures to Alibaba Cloud OSS
Introduction : This article explains how to use springboot to upload images to Alibaba Cloud OSS.
Alibaba Cloud OSS : Alibaba Cloud Object Storage Service (OSS) is a simple, secure, and scalable cloud storage service provided by Alibaba Cloud. It can store and retrieve any type of file, including text, pictures, audio, video, etc. OSS has high reliability and high availability, can store large amounts of data in the cloud, and can access and manage stored objects through simple API calls.
OSS provides a variety of storage types, including standard storage, low-frequency access storage, archive storage, etc. You can choose the appropriate storage type based on the access frequency and cost requirements of the data. At the same time, OSS also provides data encryption, access control, data migration, data backup and other functions to ensure the security and reliability of data.
Using Alibaba Cloud OSS can realize a variety of application scenarios, such as website static file storage, large-scale data backup and archiving, cloud storage and transmission, etc. Whether you are an individual developer or an enterprise user, you can store and manage your data through Alibaba Cloud OSS.
The code demonstration address of this article: https://gitee.com/geek-li-hua/code-in-blog.git
Here is the official website of Alibaba Cloud OSS: https://www.aliyun.com/product/oss
There is a free trial here
After clicking Trial, exit, search for OSS again, and then click Management Console.
common onebucket
Mainly these three places need to be configured
Code writing
Project structure
pom.xml
- Key dependencies that need to be imported
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
- Complete dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ImageUploadOss-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ImageUploadOss-demo</name>
<description>ImageUploadOss-demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Boot Starter JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Project Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- MySQL Connector/J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- MyBatis Plus Boot Starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- MyBatis Plus Generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
key code
- Then I wrote the core tool classes
public class UploadUtil {
// 阿里域名
public static final String ALI_DOMAIN= "https://test-bucket233.oss-cn-beijing.aliyuncs.com";
public static String uploadImage(MultipartFile file) throws IOException {
if (file.isEmpty()) {
return "No file uploaded"; // 如果文件为空,则返回提示信息"未上传文件"
}
String originalFilename = file.getOriginalFilename(); // 获取原始文件名
String ext='.' + originalFilename.split("\\.")[1]; // 获取文件扩展名
String uuid = UUID.randomUUID().toString().replace("-", ""); // 生成随机UUID
String newFileName = uuid + ext; // 新文件名
// 地域节点
String endpoint = "https://oss-cn-beijing.aliyuncs.com";
String accessKeyID = "xxxxxxxxxxxxxx";
String accessKeySecret = "xxxxxxxxxxxxxxxxxx";
// OSS客户端对象
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyID, accessKeySecret);
ossClient.putObject(
"test-bucket233", // 仓库名
newFileName, // 文件名
file.getInputStream());
ossClient.shutdown();
return ALI_DOMAIN + newFileName;
}
}
- Complete controller
@Controller
@RequestMapping("/url-database")
public class UrlDatabaseController {
/**
* 上传文件方法
*
* @param file 要上传的文件
* @return 返回上传结果
*/
@Autowired
private IUrlDatabaseService urlDatabaseService;
@PostMapping("/upload")
public Result addFile(MultipartFile file) throws IOException {
String path = UploadUtil.uploadImage(file);
if (path.equals("No file uploaded")) {
return Result.fail("No file uploaded");
}
System.out.println(path);
if (urlDatabaseService.save(new UrlDatabase(path))){
return Result.success(path);
} else {
return Result.fail("save fail");
}
}
// 路由器首页
@RequestMapping("/")
public String index(){
return "index.html";
}
}
After the upload is successful, it will appear in OSS.