!243 合并 oss 私有库功能
update 优化 支持 oss 私有库功能
| | |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.oss.core.OssClient; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.domain.bo.SysOssBo; |
| | | import com.ruoyi.system.domain.vo.SysOssVo; |
| | |
| | | if (ObjectUtil.isNull(file)) { |
| | | throw new ServiceException("ä¸ä¼ æä»¶ä¸è½ä¸ºç©º"); |
| | | } |
| | | SysOss oss = iSysOssService.upload(file); |
| | | SysOssVo oss = iSysOssService.upload(file); |
| | | Map<String, String> map = new HashMap<>(2); |
| | | map.put("url", oss.getUrl()); |
| | | map.put("fileName", oss.getOriginalName()); |
| | |
| | | @SaCheckPermission("system:oss:download") |
| | | @GetMapping("/download/{ossId}") |
| | | public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException { |
| | | SysOssVo sysOss = iSysOssService.getById(ossId); |
| | | if (ObjectUtil.isNull(sysOss)) { |
| | | throw new ServiceException("æä»¶æ°æ®ä¸åå¨!"); |
| | | } |
| | | FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); |
| | | response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); |
| | | long data; |
| | | try { |
| | | data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false); |
| | | } catch (HttpException e) { |
| | | if (e.getMessage().contains("403")) { |
| | | throw new ServiceException("æ 读åæé, 请å¨å¯¹åºçOSSå¼å¯'å
¬æè¯»'æé!"); |
| | | } else { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | response.setContentLength(Convert.toInt(data)); |
| | | iSysOssService.download(ossId,response); |
| | | } |
| | | |
| | | /** |
| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.MimeTypeUtils; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.domain.vo.SysOssVo; |
| | | import com.ruoyi.system.service.ISysOssService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import lombok.RequiredArgsConstructor; |
| | |
| | | if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) { |
| | | return R.fail("æä»¶æ ¼å¼ä¸æ£ç¡®ï¼è¯·ä¸ä¼ " + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "æ ¼å¼"); |
| | | } |
| | | SysOss oss = iSysOssService.upload(avatarfile); |
| | | SysOssVo oss = iSysOssService.upload(avatarfile); |
| | | String avatar = oss.getUrl(); |
| | | if (userService.updateUserAvatar(getUsername(), avatar)) { |
| | | ajax.put("imgUrl", avatar); |
| | |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.amazonaws.ClientConfiguration; |
| | | import com.amazonaws.HttpMethod; |
| | | import com.amazonaws.Protocol; |
| | | import com.amazonaws.auth.AWSCredentials; |
| | | import com.amazonaws.auth.AWSCredentialsProvider; |
| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.constant.OssConstant; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.enumd.AccessPolicyType; |
| | | import com.ruoyi.oss.enumd.PolicyType; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.properties.OssProperties; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * S3 åå¨åè®® ææå
¼å®¹S3åè®®çäºåå忝æ |
| | |
| | | return; |
| | | } |
| | | CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); |
| | | createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead); |
| | | AccessPolicyType accessPolicy = getAccessPolicy(); |
| | | createBucketRequest.setCannedAcl(accessPolicy.getAcl()); |
| | | client.createBucket(createBucketRequest); |
| | | client.setBucketPolicy(bucketName, getPolicy(bucketName, PolicyType.READ)); |
| | | client.setBucketPolicy(bucketName, getPolicy(bucketName, accessPolicy.getPolicyType())); |
| | | } catch (Exception e) { |
| | | throw new OssException("å建Bucket失败, è¯·æ ¸å¯¹é
置信æ¯:[" + e.getMessage() + "]"); |
| | | } |
| | |
| | | metadata.setContentLength(inputStream.available()); |
| | | PutObjectRequest putObjectRequest = new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata); |
| | | // 设置ä¸ä¼ 对象ç Acl 为å
Œ
±è¯» |
| | | putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); |
| | | putObjectRequest.setCannedAcl(getAccessPolicy().getAcl()); |
| | | client.putObject(putObjectRequest); |
| | | } catch (Exception e) { |
| | | throw new OssException("ä¸ä¼ æä»¶å¤±è´¥ï¼è¯·æ£æ¥é
置信æ¯:[" + e.getMessage() + "]"); |
| | |
| | | return configKey; |
| | | } |
| | | |
| | | public String getPrivateUrl(String objectKey, Integer second) { |
| | | GeneratePresignedUrlRequest generatePresignedUrlRequest = |
| | | new GeneratePresignedUrlRequest(properties.getBucketName(), objectKey) |
| | | .withMethod(HttpMethod.GET) |
| | | .withExpiration(new Date(System.currentTimeMillis() + 1000L * second)); |
| | | URL url = client.generatePresignedUrl(generatePresignedUrlRequest); |
| | | return url.toString(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åæ¡¶æéç±»å |
| | | * |
| | | * @return å½åæ¡¶æéç±»åcode |
| | | */ |
| | | public AccessPolicyType getAccessPolicy() { |
| | | return AccessPolicyType.getByType(properties.getAccessPolicy()); |
| | | } |
| | | |
| | | private static String getPolicy(String bucketName, PolicyType policyType) { |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.append("{\n\"Statement\": [\n{\n\"Action\": [\n"); |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.enumd; |
| | | |
| | | import com.amazonaws.services.s3.model.CannedAccessControlList; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * 桶访é®çç¥é
ç½® |
| | | * |
| | | * @author éè³ |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum AccessPolicyType { |
| | | |
| | | /** |
| | | * private |
| | | */ |
| | | PRIVATE("0", CannedAccessControlList.Private, PolicyType.WRITE), |
| | | |
| | | /** |
| | | * public |
| | | */ |
| | | PUBLIC("1", CannedAccessControlList.PublicRead, PolicyType.READ), |
| | | |
| | | /** |
| | | * custom |
| | | */ |
| | | CUSTOM("2",CannedAccessControlList.PublicRead, PolicyType.READ); |
| | | |
| | | /** |
| | | * æ¡¶ æéç±»å |
| | | */ |
| | | private final String type; |
| | | |
| | | /** |
| | | * æä»¶å¯¹è±¡ æéç±»å |
| | | */ |
| | | private final CannedAccessControlList acl; |
| | | |
| | | /** |
| | | * æ¡¶çç¥ç±»å |
| | | */ |
| | | private final PolicyType policyType; |
| | | |
| | | public static AccessPolicyType getByType(String type) { |
| | | for (AccessPolicyType value : values()) { |
| | | if (value.getType().equals(type)) { |
| | | return value; |
| | | } |
| | | } |
| | | throw new RuntimeException("'type' not found By " + type); |
| | | } |
| | | |
| | | } |
| | |
| | | */ |
| | | private String isHttps; |
| | | |
| | | /** |
| | | * æ¡¶æéç±»å(0private 1public 2custom) |
| | | */ |
| | | private String accessPolicy; |
| | | |
| | | } |
| | |
| | | */ |
| | | private String remark; |
| | | |
| | | /** |
| | | * æ¡¶æéç±»å(0private 1public 2custom) |
| | | */ |
| | | private String accessPolicy; |
| | | } |
| | |
| | | */ |
| | | private String remark; |
| | | |
| | | /** |
| | | * æ¡¶æéç±»å(0private 1public 2custom) |
| | | */ |
| | | @NotBlank(message = "æ¡¶æéç±»åä¸è½ä¸ºç©º", groups = {AddGroup.class, EditGroup.class}) |
| | | private String accessPolicy; |
| | | |
| | | } |
| | |
| | | */ |
| | | private String remark; |
| | | |
| | | /** |
| | | * æ¡¶æéç±»å(0private 1public 2custom) |
| | | */ |
| | | private String accessPolicy; |
| | | |
| | | } |
| | |
| | | import com.ruoyi.system.domain.vo.SysOssVo; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | SysOssVo getById(Long ossId); |
| | | |
| | | SysOss upload(MultipartFile file); |
| | | SysOssVo upload(MultipartFile file); |
| | | |
| | | void download(Long ossId, HttpServletResponse response) throws IOException; |
| | | |
| | | Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid); |
| | | |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpException; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.BeanCopyUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.oss.core.OssClient; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.enumd.AccessPolicyType; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.domain.bo.SysOssBo; |
| | |
| | | import com.ruoyi.system.service.ISysOssService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.cache.annotation.Cacheable; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ æå¡å±å®ç° |
| | |
| | | public TableDataInfo<SysOssVo> queryPageList(SysOssBo bo, PageQuery pageQuery) { |
| | | LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo); |
| | | Page<SysOssVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw); |
| | | List<SysOssVo> filterResult = result.getRecords().stream().map(this::matchingUrl).collect(Collectors.toList()); |
| | | result.setRecords(filterResult); |
| | | return TableDataInfo.build(result); |
| | | } |
| | | |
| | |
| | | for (Long id : ossIds) { |
| | | SysOssVo vo = SpringUtils.getAopProxy(this).getById(id); |
| | | if (ObjectUtil.isNotNull(vo)) { |
| | | list.add(vo); |
| | | list.add(this.matchingUrl(vo)); |
| | | } |
| | | } |
| | | return list; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public SysOss upload(MultipartFile file) { |
| | | public void download(Long ossId, HttpServletResponse response) throws IOException { |
| | | SysOssVo sysOss = this.matchingUrl(SpringUtils.getAopProxy(this).getById(ossId)); |
| | | if (ObjectUtil.isNull(sysOss)) { |
| | | throw new ServiceException("æä»¶æ°æ®ä¸åå¨!"); |
| | | } |
| | | FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName()); |
| | | response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8"); |
| | | long data; |
| | | try { |
| | | data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false); |
| | | } catch (HttpException e) { |
| | | if (e.getMessage().contains("403")) { |
| | | throw new ServiceException("æ 读åæé, 请å¨å¯¹åºçOSSå¼å¯'å
¬æè¯»'æé!"); |
| | | } else { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | response.setContentLength(Convert.toInt(data)); |
| | | } |
| | | |
| | | @Override |
| | | public SysOssVo upload(MultipartFile file) { |
| | | String originalfileName = file.getOriginalFilename(); |
| | | String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); |
| | | OssClient storage = OssFactory.instance(); |
| | |
| | | oss.setOriginalName(originalfileName); |
| | | oss.setService(storage.getConfigKey()); |
| | | baseMapper.insert(oss); |
| | | return oss; |
| | | SysOssVo sysOssVo = new SysOssVo(); |
| | | BeanCopyUtils.copy(oss, sysOssVo); |
| | | return this.matchingUrl(sysOssVo); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return baseMapper.deleteBatchIds(ids) > 0; |
| | | } |
| | | |
| | | /** |
| | | * å¹é
Url |
| | | * |
| | | * @param oss OSS对象 |
| | | * @return oss å¹é
UrlçOSS对象 |
| | | */ |
| | | private SysOssVo matchingUrl(SysOssVo oss) { |
| | | OssClient storage = OssFactory.instance(oss.getService()); |
| | | // ä»
ä¿®æ¹æ¡¶ç±»å为 private çURLï¼ä¸´æ¶URLæ¶é¿ä¸º120s |
| | | if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) { |
| | | oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 120)); |
| | | } |
| | | return oss; |
| | | } |
| | | } |
| | |
| | | <el-table-column label="æ¡¶åç§°" align="center" prop="bucketName" /> |
| | | <el-table-column label="åç¼" align="center" prop="prefix" /> |
| | | <el-table-column label="å" align="center" prop="region" /> |
| | | <el-table-column label="æ¡¶æéç±»å" align="center" prop="accessPolicy" :formatter="accessPolicyStateFormat" /> |
| | | <el-table-column label="ç¶æ" align="center" prop="status"> |
| | | <template slot-scope="scope"> |
| | | <el-switch |
| | |
| | | :key="dict.value" |
| | | :label="dict.value" |
| | | >{{dict.label}}</el-radio> |
| | | </el-radio-group> |
| | | </el-form-item> |
| | | <el-form-item label="æ¡¶æéç±»å"> |
| | | <el-radio-group v-model="form.accessPolicy"> |
| | | <el-radio label="0">private</el-radio> |
| | | <el-radio label="1">public</el-radio> |
| | | <el-radio label="2">custom</el-radio> |
| | | </el-radio-group> |
| | | </el-form-item> |
| | | <el-form-item label="å" prop="region"> |
| | |
| | | trigger: "blur", |
| | | }, |
| | | ], |
| | | accessPolicy:[ |
| | | { required: true, message: "accessPolicyä¸è½ä¸ºç©º", trigger: "blur" } |
| | | ] |
| | | }, |
| | | }; |
| | | }, |
| | |
| | | endpoint: undefined, |
| | | domain: undefined, |
| | | isHttps: "N", |
| | | accessPolicy: "1", |
| | | region: undefined, |
| | | status: "1", |
| | | remark: undefined, |
| | |
| | | }).catch(() => { |
| | | row.status = row.status === "0" ? "1" : "0"; |
| | | }) |
| | | }, |
| | | accessPolicyStateFormat(row) { |
| | | if (row.accessPolicy === "0") { |
| | | return <span class="el-tag el-tag--warning el-tag--medium el-tag--light">private</span> |
| | | } else if (row.accessPolicy === "1") { |
| | | return <span class="el-tag el-tag--success el-tag--medium el-tag--light">public</span> |
| | | } else if (row.accessPolicy === "2") { |
| | | return <span class="el-tag el-tag--medium el-tag--light">custom</span> |
| | | } |
| | | |
| | | } |
| | | } |
| | | }; |
| | |
| | | domain varchar(255) default '' comment 'èªå®ä¹åå', |
| | | is_https char(1) default 'N' comment 'æ¯å¦httpsï¼Y=æ¯,N=å¦ï¼', |
| | | region varchar(255) default '' comment 'å', |
| | | access_policy char(1) not null default '1' comment 'æ¡¶æéç±»å(0=private 1=public 2=custom)', |
| | | status char(1) default '1' comment 'ç¶æï¼0=æ£å¸¸,1=åç¨ï¼', |
| | | ext1 varchar(255) default '' comment 'æ©å±å段', |
| | | create_by varchar(64) default '' comment 'å建è
', |
| | |
| | | primary key (oss_config_id) |
| | | ) engine=innodb comment='对象åå¨é
置表'; |
| | | |
| | | insert into sys_oss_config values (1, 'minio', 'ruoyi', 'ruoyi123', 'ruoyi', '', '127.0.0.1:9000', '','N', '', '0', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (2, 'qiniu', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi', '', 's3-cn-north-1.qiniucs.com', '','N', '', '1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (3, 'aliyun', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi', '', 'oss-cn-beijing.aliyuncs.com', '','N', '', '1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (4, 'qcloud', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi-1250000000', '', 'cos.ap-beijing.myqcloud.com', '','N', 'ap-beijing', '1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (5, 'image', 'ruoyi', 'ruoyi123', 'ruoyi', 'image', '127.0.0.1:9000', '','N', '', '1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | |
| | | insert into sys_oss_config values (1, 'minio', 'ruoyi', 'ruoyi123', 'ruoyi', '', '127.0.0.1:9000', '','N', '', '1' ,'0', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (2, 'qiniu', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi', '', 's3-cn-north-1.qiniucs.com', '','N', '', '1' ,'1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (3, 'aliyun', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi', '', 'oss-cn-beijing.aliyuncs.com', '','N', '', '1' ,'1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (4, 'qcloud', 'XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXX', 'ruoyi-1250000000', '', 'cos.ap-beijing.myqcloud.com', '','N', 'ap-beijing', '1' ,'1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |
| | | insert into sys_oss_config values (5, 'image', 'ruoyi', 'ruoyi123', 'ruoyi', 'image', '127.0.0.1:9000', '','N', '', '1' ,'1', '', 'admin', sysdate(), 'admin', sysdate(), NULL); |