Skip to content
大纲

OSS 文件上传

流程介绍

通过文件上传接口如 ProductController.java

java
/**
	 * 上传商品图片、视频
	 */
	@ApiOperation(value = "上传商品图片、视频", notes = "上传商品图片、视频")
	@ApiOperationSupport(order = 2)
	@PostMapping("/upload_product_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;
		}
		ProductImage productImage = new ProductImage();
		String productVideo=new String();
		if(file.getContentType().indexOf("image") != -1 ||  file.getContentType().indexOf("application")!=-1){
			//图片上传
			productImage = productImageService.generateImage(file,path);
			productImage.setType(ProductImage.Type.IMAGE);
		}else if(file.getContentType().indexOf("video") != -1){
			//视频上传
			productImage = productImageService.generateVideo(file,path);
			productImage.setType(ProductImage.Type.MEDIA);
		}
		if (productImage == null&&CommonUtils.isEmpty(productVideo)) {
			return R.unprocessableEntity("common_upload_error");
		}
		return R.ok(productImage);
	}
  • 文件上传相关配置(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还要配置跨域,如下面图片
  • 后台 -> 系统管理 -> 文件存储配置 -> 配置存储账号

image.png

  • minio需要自建服务,用法和阿里OSS类似;觉得阿里OSS、七牛云收费贵不想用的,可以使用minio
  • minio的accessKeyId、accessKeySecret就是minio后台的账号、密码

后台参数对应(endpoint、bucket)

1.MINIO
要保证minio地址外网能访问image.png

2.腾讯COS
image.png

3.阿里OSS
image.png

4.七牛云
image.png

版权许可