阿里云文件上传的完整步骤:
一:先注册阿里云账号,创建好对象存储的空间
1.没有账号的小伙伴先点击注册,根据手机号注册登录
2.创建Bucket,名字需要记住
3.创建AccessKey
阿里云已经创建好了,接下来我们来实现代码
代码实现
二:实现步骤
1.在pom.xml文件中导入坐标
- <dependency>
- <groupId>com.aliyun.oss</groupId>
- <artifactId>aliyun-sdk-oss</artifactId>
- <version>${aliyun.sdk.oss}</version>
- </dependency>
2.在yml文件配置对应属性
3.定义一个OSS的配置的实体类
- package com.sky.properties;
-
- import lombok.Data;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- @Component
- @ConfigurationProperties(prefix = "sky.alioss")
- @Data
- public class AliOssProperties {
-
- private String endpoint;
- private String accessKeyId;
- private String accessKeySecret;
- private String bucketName;
-
- }
4.定义一个OSS的配置类
- package com.sky.config;
-
- import com.sky.properties.AliOssProperties;
- import com.sky.utils.AliOssUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * 配置类,用于创建AliOssUtil对象
- */
- @Configuration
- @Slf4j
- public class OssConfiguration {
-
- @Bean
- @ConditionalOnMissingBean
- public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
- log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);
- return new AliOssUtil(aliOssProperties.getEndpoint(),
- aliOssProperties.getAccessKeyId(),
- aliOssProperties.getAccessKeySecret(),
- aliOssProperties.getBucketName());
- }
- }
5.定义utils工具类
-
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.util.UUID;
-
- /**
- * 阿里云 OSS 工具类
- */
- public class AliOSSUtils {
-
- @Value("${aliyun.oss.endpoint}")
- private String endpoint;
- @Value("${aliyun.oss.accessKeyId}")
- private String accessKeyId;
- @Value("${aliyun.oss.accessKeySecret}")
- private String accessKeySecret;
- @Value("${aliyun.oss.bucketName}")
- private String bucketName;
-
- /**
- * 实现上传图片到OSS
- */
- public String upload(MultipartFile file) throws IOException {
- // 获取上传的文件的输入流
- InputStream inputStream = file.getInputStream();
-
- // 避免文件覆盖
- String originalFilename = file.getOriginalFilename();
- String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
-
- //上传文件到 OSS
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- ossClient.putObject(bucketName, fileName, inputStream);
-
- //文件访问路径
- String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
- // 关闭ossClient
- ossClient.shutdown();
- return url;// 把上传到oss的路径返回
- }
-
- }
6.实现controller层代码
- /**
- * 通用接口
- */
- @RestController
- @RequestMapping("/admin/common")
- @Api(tags = "通用接口")
- @Slf4j
- public class CommonController {
-
- @Autowired
- private AliOssUtil aliOssUtil;
-
- /**
- * 文件上传
- * @param file
- * @return
- */
- @PostMapping("/upload")
- @ApiOperation("文件上传")
- public Result
upload(MultipartFile file){ - log.info("文件上传:{}",file);
-
- try {
- //原始文件名
- String originalFilename = file.getOriginalFilename();
- //截取原始文件名的后缀 dfdfdf.png
- String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
- //构造新文件名称
- String objectName = UUID.randomUUID().toString() + extension;
-
- //文件的请求路径
- String filePath = aliOssUtil.upload(file.getBytes(), objectName);
- return Result.success(filePath);
- } catch (IOException e) {
- log.error("文件上传失败:{}", e);
- }
-
- return Result.error(MessageConstant.UPLOAD_FAILED);
- }
- }