| | |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.lang.UUID; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.model.CannedAccessControlList; |
| | | import com.aliyun.oss.model.CreateBucketRequest; |
| | | import com.aliyun.oss.model.PutObjectRequest; |
| | | import com.aliyun.oss.model.PutObjectResult; |
| | | import com.sandu.common.enums.FileStorageEnums; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.file.FileUploadDto; |
| | | import com.sandu.common.file.IFileUpload; |
| | | import lombok.AllArgsConstructor; |
| | | import com.sandu.common.file.config.minioConfig.MinIoPolicy; |
| | | import io.minio.*; |
| | | import io.minio.messages.Bucket; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.math.BigInteger; |
| | | import java.security.MessageDigest; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | | * @date 2020/12/17 15:31 |
| | | */ |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | @Component |
| | | public class AliOssFileServiceImpl implements IFileUpload { |
| | | |
| | | private final String endPoint; |
| | | private final String keyId; |
| | | private final String keySecret; |
| | | private final String bucketName; |
| | | private final String host; |
| | | @Autowired |
| | | private MinioClient minioClient; |
| | | |
| | | private String bucketName = "remote-update"; |
| | | public String MINIO_PREFIX = "vnnox."; |
| | | |
| | | |
| | | @Override |
| | | public FileUploadDto uploadFile(MultipartFile multipartFile) { |
| | |
| | | long size = multipartFile.getSize(); |
| | | String suffix = FileUtil.getSuffix(originalFilename); |
| | | String dayStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")); |
| | | String fileName = dayStr + UUID.fastUUID().toString() + "." + suffix; |
| | | String fileName = dayStr + UUID.fastUUID() + "." + suffix; |
| | | |
| | | String md5 = getMD5(multipartFile); |
| | | FileUploadDto uploadDto = new FileUploadDto(); |
| | | OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret); |
| | | try { |
| | | //容器不存在,就创建 |
| | | if (!ossClient.doesBucketExist(bucketName)) { |
| | | ossClient.createBucket(bucketName); |
| | | CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); |
| | | createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead); |
| | | ossClient.createBucket(createBucketRequest); |
| | | } |
| | | //上传文件 |
| | | PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileName, new ByteArrayInputStream(multipartFile.getBytes()))); |
| | | //设置权限 这里是公开读 |
| | | // ossClient.setBucketAcl(ossProperties.getBucketName(), CannedAccessControlList.PublicRead); |
| | | |
| | | if (result != null) { |
| | | String url = ""; |
| | | // https://BucketName.Endpoint/ObjectName |
| | | if (StrUtil.isBlank(host)) { |
| | | url = "https://" + bucketName + "." + endPoint + "/" + fileName; |
| | | } else { |
| | | if (!host.endsWith("/")) { |
| | | url = host + "/" + fileName; |
| | | } else { |
| | | url = host + fileName; |
| | | } |
| | | |
| | | try { |
| | | if (!bucketExists(bucketName)) { |
| | | createBucket(bucketName); |
| | | if (bucketName.contains(MINIO_PREFIX)) { |
| | | setBucketPolicy(bucketName); |
| | | } |
| | | uploadDto.setFileUrl(url); |
| | | } |
| | | |
| | | final InputStream is = multipartFile.getInputStream(); |
| | | Long start = System.currentTimeMillis(); |
| | | minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1)); |
| | | is.close(); |
| | | System.out.println(("上传总时间:" + (System.currentTimeMillis() - start) + "ms")); |
| | | |
| | | // 从minio获取 url |
| | | String url = minioClient.presignedGetObject(bucketName, fileName); |
| | | System.out.println("获取到的路径:"+url); |
| | | uploadDto.setFileUrl(url); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | throw new BusinessException(e.getMessage()); |
| | | } finally { |
| | | //关闭 |
| | | ossClient.shutdown(); |
| | | } |
| | | |
| | | uploadDto.setOriginName(originalFilename); |
| | |
| | | uploadDto.setFileType(FileUtil.extName(originalFilename)); |
| | | uploadDto.setStorageType(FileStorageEnums.ALIBABA.getCode()); |
| | | uploadDto.setFileSize(size); |
| | | uploadDto.setMd5(md5); |
| | | |
| | | if (IMAGE_SUFFIX_LIST.contains(uploadDto.getFileType())) { |
| | | BufferedImage img = null; |
| | |
| | | uploadDto.setHeight(img.getHeight()); |
| | | } |
| | | } |
| | | |
| | | return uploadDto; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取上传文件的MD5值 |
| | | * |
| | | * @param file |
| | | * @return |
| | | */ |
| | | public String getMD5(MultipartFile file) { |
| | | |
| | | try { |
| | | byte[] uploadBytes = file.getBytes(); |
| | | MessageDigest md5 = MessageDigest.getInstance("MD5"); |
| | | byte[] digest = md5.digest(uploadBytes); |
| | | String hashString = new BigInteger(1, digest).toString(16); |
| | | if (hashString.length() % 2 != 0) { |
| | | hashString = "0" + hashString; |
| | | } |
| | | return hashString; |
| | | } catch (Exception e) { |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 设置开放*的桶策略 |
| | | * |
| | | * @param bucketName |
| | | * @return |
| | | */ |
| | | public void setBucketPolicy(String bucketName) throws Exception { |
| | | minioClient.setBucketPolicy( |
| | | SetBucketPolicyArgs.builder().bucket(bucketName).config(MinIoPolicy.generateMinioPolicy(bucketName)).build() |
| | | ); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断 bucket是否存在 |
| | | * |
| | | * @param bucketName: 桶名 |
| | | * @return: boolean |
| | | */ |
| | | @SneakyThrows(Exception.class) |
| | | public boolean bucketExists(String bucketName) { |
| | | return minioClient.bucketExists(bucketName); |
| | | } |
| | | |
| | | public boolean objectExists(String bucketName, String objectName) { |
| | | try { |
| | | getObjectInfo(bucketName, objectName); |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | private void getObjectInfo(String bucketName, String objectName) { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 创建 bucket |
| | | * |
| | | * @param bucketName: 桶名 |
| | | * @return: void |
| | | */ |
| | | @SneakyThrows(Exception.class) |
| | | public void createBucket(String bucketName) { |
| | | boolean isExist = minioClient.bucketExists((BucketExistsArgs) ((io.minio.BucketExistsArgs.Builder) BucketExistsArgs.builder().bucket(bucketName)).build()); |
| | | if (!isExist) { |
| | | minioClient.makeBucket((MakeBucketArgs) ((io.minio.MakeBucketArgs.Builder) MakeBucketArgs.builder().bucket(bucketName)).build()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取全部bucket |
| | | */ |
| | | @SneakyThrows(Exception.class) |
| | | public List<Bucket> getAllBuckets() { |
| | | return minioClient.listBuckets(); |
| | | } |
| | | |
| | | } |