forked from macrozheng/mall-swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de89d82
commit 46e6170
Showing
6 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
mall-admin/src/main/java/com/macro/mall/controller/MinioController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.macro.mall.controller; | ||
|
||
import com.macro.mall.common.api.CommonResult; | ||
import com.macro.mall.dto.MinioUploadDto; | ||
import io.minio.MinioClient; | ||
import io.minio.policy.PolicyType; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
/** | ||
* Created by macro on 2019/12/25. | ||
*/ | ||
@Api(tags = "MinioController", description = "MinIO对象存储管理") | ||
@Controller | ||
@RequestMapping("/minio") | ||
public class MinioController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MinioController.class); | ||
@Value("${minio.endpoint}") | ||
private String ENDPOINT; | ||
@Value("${minio.bucketName}") | ||
private String BUCKET_NAME; | ||
@Value("${minio.accessKey}") | ||
private String ACCESS_KEY; | ||
@Value("${minio.secretKey}") | ||
private String SECRET_KEY; | ||
|
||
@ApiOperation("文件上传") | ||
@RequestMapping(value = "/upload", method = RequestMethod.POST) | ||
@ResponseBody | ||
public CommonResult upload(@RequestParam("file") MultipartFile file) { | ||
try { | ||
//创建一个MinIO的Java客户端 | ||
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY); | ||
boolean isExist = minioClient.bucketExists(BUCKET_NAME); | ||
if (isExist) { | ||
LOGGER.info("存储桶已经存在!"); | ||
} else { | ||
//创建存储桶并设置只读权限 | ||
minioClient.makeBucket(BUCKET_NAME); | ||
minioClient.setBucketPolicy(BUCKET_NAME, "*.*", PolicyType.READ_ONLY); | ||
} | ||
String filename = file.getOriginalFilename(); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); | ||
// 设置存储对象名称 | ||
String objectName = sdf.format(new Date()) + "/" + filename; | ||
// 使用putObject上传一个文件到存储桶中 | ||
minioClient.putObject(BUCKET_NAME, objectName, file.getInputStream(), file.getContentType()); | ||
LOGGER.info("文件上传成功!"); | ||
MinioUploadDto minioUploadDto = new MinioUploadDto(); | ||
minioUploadDto.setName(filename); | ||
minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName); | ||
return CommonResult.success(minioUploadDto); | ||
} catch (Exception e) { | ||
LOGGER.info("上传发生错误: {}!", e.getMessage()); | ||
} | ||
return CommonResult.failed(); | ||
} | ||
|
||
@ApiOperation("文件删除") | ||
@RequestMapping(value = "/delete", method = RequestMethod.POST) | ||
@ResponseBody | ||
public CommonResult delete(@RequestParam("objectName") String objectName) { | ||
try { | ||
MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY); | ||
minioClient.removeObject(BUCKET_NAME, objectName); | ||
return CommonResult.success(null); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return CommonResult.failed(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
mall-admin/src/main/java/com/macro/mall/dto/MinioUploadDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.macro.mall.dto; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
/** | ||
* 文件上传返回结果 | ||
* Created by macro on 2019/12/25. | ||
*/ | ||
@Data | ||
@EqualsAndHashCode(callSuper = false) | ||
public class MinioUploadDto { | ||
private String url; | ||
private String name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters