Skip to content

Commit

Permalink
minio小程序文件上传
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenWatermelon committed Apr 19, 2022
1 parent fa73b9e commit f7f3ed8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
import com.mall4j.cloud.common.response.ResponseEnum;
import io.minio.*;
import io.minio.http.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -21,6 +27,8 @@ public class MinioTemplate implements InitializingBean {

private MinioClient minioClient;

static final Logger logger = LoggerFactory.getLogger(MinioTemplate.class);

@Override
public void afterPropertiesSet() throws Exception {
this.minioClient = MinioClient.builder().endpoint(ossConfig.getEndpoint())
Expand Down Expand Up @@ -52,4 +60,25 @@ public String getPresignedObjectUrl(String objectName){
throw new Mall4cloudException(ResponseEnum.EXCEPTION);
}
}

public void uploadMinio(byte[] bytes, String filePath, String contentType) throws IOException {
InputStream input = null;
try {
input = new ByteArrayInputStream(bytes);
minioClient.putObject(
PutObjectArgs.builder()
.bucket(ossConfig.getBucket())
.contentType(contentType)
.stream(input, input.available(), -1)
.object(filePath)
.build()
);
} catch (Exception e) {
logger.error("minio上传文件错误:", e);
} finally {
if (Objects.nonNull(input)) {
input.close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -76,4 +75,17 @@ private OssVO loadOssVO(OssVO ossVo) {
return ossVo;
}


@PostMapping("/upload_minio")
@ApiOperation(value = "文件上传接口", notes = "上传文件,返回文件路径与域名")
public ServerResponseEntity<OssVO> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
return ServerResponseEntity.success();
}
OssVO oss = loadOssVO(new OssVO());
minioTemplate.uploadMinio(file.getBytes(), oss.getDir() + oss.getFileName(), file.getContentType());
return ServerResponseEntity.success(oss);
}


}

0 comments on commit f7f3ed8

Please sign in to comment.