Appearance
OSS 文件上传
流程介绍
通过文件上传接口如 GoodsController.java
java
/**
* 上传商品图片、视频
*/
@ApiOperation(value = "上传商品图片、视频", notes = "上传商品图片、视频")
@ApiOperationSupport(order = 2)
@PostMapping("/upload_goods_image")
public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam("path") String path) throws Exception {
path = StrUtil.format("{}/{}/", TenantContextHolder.getTenantId(), path);
ConfigStorage configStorage = feignConfigStorageService.getOne(SecurityEnum.FROM_IN);
if(configStorage == null){
throw new RuntimeException("请先配置文件存储信息");
}
if (file == null || file.isEmpty()) {
return R.UNPROCESSABLE_ENTITY;
}
GoodsImage goodsImage = new GoodsImage();
String goodsVideo=new String();
if(file.getContentType().indexOf("image") != -1 || file.getContentType().indexOf("application")!=-1){
//图片上传
goodsImage = goodsImageService.generateImage(file,path);
goodsImage.setType(GoodsImage.Type.IMAGE);
}else if(file.getContentType().indexOf("video") != -1){
//视频上传
goodsImage = goodsImageService.generateVideo(file,path);
goodsImage.setType(GoodsImage.Type.MEDIA);
}
if (goodsImage == null&&CommonUtils.isEmpty(goodsVideo)) {
return R.unprocessableEntity("common_upload_error");
}
return R.ok(goodsImage);
}
- 文件上传相关配置(application-dev.yml),如文件大小设置
Nginx 文件上传设置
正式环境还需在nginx中配置文件上传的相关参数,比如文件大小限制。
properties
server {
listen 80;
server_name demo.xxxxxx.cn;
proxy_buffering on;
proxy_buffers 2 512k;
proxy_buffer_size 512k;
proxy_busy_buffers_size 512k;
proxy_temp_path /tmp/nginx_proxy_tmp 1 2;
proxy_max_temp_file_size 20M;
proxy_temp_file_write_size 512k;
root /www/xxxxxx;
location / {
集成SKD
- 七牛云
- 阿里OSS
- minio
- 腾讯cos v2.7.1
配置
- 阿里OSS、腾讯cos、七牛云请去相应官网申请账号
- 注意:必须设为公共读,阿里OSS还要配置跨域,如下面图片
- 后台 -> 系统管理 -> 文件存储配置 -> 配置存储账号
- minio需要自建服务,用法和阿里OSS类似;觉得阿里OSS、七牛云收费贵不想用的,可以使用minio
- minio的accessKeyId、accessKeySecret就是minio后台的账号、密码