<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.1.0</version> </dependency>
#服务端口 server.port=8002 #服务名 spring.application.name=service-oss #环境设置:dev、test、prod spring.profiles.active=dev #阿里云 OSS #不同的服务器,地址不同 aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com aliyun.oss.file.keyid=自己的keyid aliyun.oss.file.keysecret=自己的keysecret #bucket可以在控制台创建,也可以使用java代码创建 aliyun.oss.file.bucketname=自己创建的bucket名字
package com.gyb.eduoss.utils; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Author: 郜宇博 * @Date: 2021/9/30 14:54 */ @Component @ConfigurationProperties(prefix = "aliyun.oss.file") @Data public class AliyunOSSProUtil { private String endpoint; private String keyid; private String keysecret; private String bucketname; }
package com.gyb.eduoss.service.impl; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.gyb.eduoss.service.OssService; import com.gyb.eduoss.utils.AliyunOSSProUtil; import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; /** * @Author: 郜宇博 * @Date: 2021/9/30 15:53 */ @Service public class OssServiceImpl implements OssService { @Autowired AliyunOSSProUtil aliyunOSSProUtil; @Override public String uploadFileAvatar(MultipartFile multipartFile) { //从工具类获取配置文件中的oss值 String endpoint = aliyunOSSProUtil.getEndpoint(); String accessKeyId = aliyunOSSProUtil.getKeyid(); String accessKeySecret = aliyunOSSProUtil.getKeysecret(); String bucketName = aliyunOSSProUtil.getBucketname(); System.out.println("endponit:"+endpoint); // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 填写本地文件的完整路径。 // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 InputStream inputStream; try { inputStream = multipartFile.getInputStream(); //参数一:依次填写Bucket名称 // 参数二:文件名称,或Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 // 参数三:输入流 String currentTime = String.valueOf(System.currentTimeMillis()); String currentDate = new DateTime().toString("yyyy/MM/dd"); String originalFileName = multipartFile.getOriginalFilename(); String fileName = currentDate+"/"+currentTime+originalFileName; ossClient.putObject(bucketName,fileName, inputStream); String url = "https://"+bucketName+"."+endpoint+"/"+fileName; return url; } catch (IOException e) { e.printStackTrace(); }finally { // 关闭OSSClient。 ossClient.shutdown(); } return null; } }
package com.gyb.eduoss.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; /** * @Author: 郜宇博 * @Date: 2021/10/2 00:21 */ @Configuration public class UploadConfig { @Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("UTF-8"); //resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常 resolver.setResolveLazily(true); resolver.setMaxInMemorySize(40960); //上传文件大小 5M 5*1024*1024 resolver.setMaxUploadSize(5 * 1024 * 1024); return resolver; } }
下载Nginx
实现将对应访问地址进行正则匹配转发
server { listen 9001; server_name localhost; #代表带有eduservice路径的请求都走http://localhost:8001 location ~ /eduservice/ { proxy_pass http://localhost:8001; } #代表带有eduoss的请求都走http://localhost:8002 location ~ /eduoss/ { proxy_pass http://localhost:8002; } }
此时将前端的访问接口改为9001,即可实现下图功能
从而实现功能不同的请求访问不同的服务器