赞
踩
1.开始上传文件
文档首页 > 阿里云学习路径 > OSS 学习路径
项目地址: https://gitee.com/zysheep/springcloudalibaba.git
修改pom.xml,引入阿里云oss依赖
<dependencies>
<dependency>
<groupId>cn.zysheep</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--阿里云oss依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<!--日期工具类-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
添加配置文件
server:
port: 9993
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: server-oss
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
aliyun:
oss:
endpoint: oss-cn-guangzhou.aliyuncs.com
bucket: yygh-zysheep
accessKeyId: xxxxxxx #阿里云accessKeyId
secret: xxxxxxx #阿里云secret
@SpringBootApplication
@EnableDiscoveryClient
public class ServerOssApplication {
public static void main(String[] args) {
SpringApplication.run(ServerOssApplication.class,args);
}
}
server:
port: 9999
spring:
application:
name: service-gateway
# nacos服务地址
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
#使用服务发现路由
discovery:
locator:
enabled: true
routes:
#设置路由id
- id: service-consumer #路由的ID,没有固定规则但要求唯一,简易配合服务名
#设置路由的uri
uri: lb://service-consumer #匹配后提供服务的路由地址,lb后跟提供服务的微服务的名,不要写错
#设置路由断言,代理servicerId为service-cosumer的/api/路径
predicates:
- Path=/*/consumer/** #断言,路径相匹配的进行路由
- id: service-provider #路由的ID,没有固定规则但要求唯一,简易配合服务名
#设置路由的uri
uri: lb://service-provider #匹配后提供服务的路由地址,lb后跟提供服务的微服务的名,不要写错
#设置路由断言,代理servicerId为service-cosumer的/api/路径
predicates:
- Path=/*/provider/** #断言,路径相匹配的进行路由
- id: server-msm
uri: lb://server-msm
predicates:
- Path=/*/msm/**
- id: server-oss
uri: lb://server-oss
predicates:
- Path=/*/oss/**
创建存储空间
public class OssTest {
public static void main(String[] args) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
String accessKeyId = "xxxxx";
String accessKeySecret = "xxxx";
String bucketName = "yygh-zysheep-testoss";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 创建存储空间。
ossClient.createBucket(bucketName);
// 关闭OSSClient。
ossClient.shutdown();
}
}
1.创建FileService上传接口
public interface FileService {
//上传文件到阿里云oss
String upload(MultipartFile file);
}
2.创建ConstantOssPropertiesUtils配置类
@Component
public class ConstantOssPropertiesUtils implements InitializingBean {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.secret}")
private String secret;
@Value("${aliyun.oss.bucket}")
private String bucket;
public static String EDNPOINT;
public static String ACCESS_KEY_ID;
public static String SECRECT;
public static String BUCKET;
@Override
public void afterPropertiesSet() throws Exception {
EDNPOINT=endpoint;
ACCESS_KEY_ID=accessKeyId;
SECRECT=secret;
BUCKET=bucket;
}
}
3.创建接口类实现类
@Service
public class FileServiceImpl implements FileService {
@Override
public String upload(MultipartFile file) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
String bucketName = ConstantOssPropertiesUtils.BUCKET;
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
//生成随机唯一值,使用uuid,添加到文件名称里面
String uuid = UUID.randomUUID().toString().replaceAll("-","");
fileName = uuid+fileName;
//按照当前日期,创建文件夹,上传到创建文件夹里面
// 2021/02/02/01.jpg
String timeUrl = new DateTime().toString("yyyy/MM/dd");
fileName = timeUrl+"/"+fileName;
//调用方法实现上传
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//上传之后文件路径
// https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
//返回
return url;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
返回上传成功后的url地址,在前端显示
@RestController
@RequestMapping("/api/oss/file")
public class FileApiController {
@Autowired
private FileService fileService;
//上传文件到阿里云oss
@PostMapping("fileUpload")
public Result fileUpload(MultipartFile file) {
//获取上传文件
String url = fileService.upload(file);
return Result.ok(url);
}
}
1.访问http://localhost:9993/swagger-ui.html
2.try it out
3.查看阿里云oss控制台,上传成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。