mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
阿里云oss
This commit is contained in:
23
pom.xml
23
pom.xml
@@ -54,6 +54,29 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-quartz</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.15.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<!-- no more than 2.3.3-->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.3.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup</groupId>
|
||||
<artifactId>javapoet</artifactId>
|
||||
|
||||
@@ -11,10 +11,12 @@ import cn.lihongjie.coal.file.dto.UpdateFileDto;
|
||||
import cn.lihongjie.coal.file.entity.FileEntity;
|
||||
import cn.lihongjie.coal.file.mapper.FileMapper;
|
||||
import cn.lihongjie.coal.file.repository.FileRepository;
|
||||
import cn.lihongjie.coal.spring.config.AliyunProperty;
|
||||
import cn.lihongjie.coal.spring.config.HwCloudProperty;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.PutObjectResult;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -41,6 +43,12 @@ import java.util.UUID;
|
||||
public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
@Autowired ObsClient obsClient;
|
||||
@Autowired HwCloudProperty hwCloudProperty;
|
||||
|
||||
@Autowired
|
||||
AliyunProperty aliyunProperty;
|
||||
|
||||
@Autowired OSSClient ossClient;
|
||||
|
||||
@Autowired private FileRepository repository;
|
||||
@Autowired private FileMapper mapper;
|
||||
@Autowired private ConversionService conversionService;
|
||||
@@ -86,6 +94,8 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
|
||||
return page.map(this.mapper::toDto);
|
||||
}
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@SneakyThrows
|
||||
public FileDto upload(MultipartFile file, String name, String dir, String sortKey, String code, String remarks) {
|
||||
@@ -113,17 +123,33 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
fileEntity.setMimeType(tika.detect(inputStream));
|
||||
}
|
||||
|
||||
PutObjectResult putObjectResult =
|
||||
obsClient.putObject(
|
||||
hwCloudProperty.getObs().getBucketName(),
|
||||
fileEntity.getDirectory() + "/" + fileEntity.getObjectId(),
|
||||
file.getInputStream());
|
||||
String objectKey = fileEntity.getDirectory() + "/" + fileEntity.getObjectId();
|
||||
String objectUrl = "";
|
||||
// PutObjectResult putObjectResult =
|
||||
// obsClient.putObject(
|
||||
// hwCloudProperty.getObs().getBucketName(),
|
||||
// objectKey,
|
||||
// file.getInputStream());
|
||||
//
|
||||
// if (putObjectResult.getStatusCode() != 200) {
|
||||
// throw new BizException("上传文件失败 %s".formatted(putObjectResult.toString()));
|
||||
// }
|
||||
|
||||
if (putObjectResult.getStatusCode() != 200) {
|
||||
throw new BizException("上传文件失败 %s".formatted(putObjectResult.toString()));
|
||||
// objectUrl = putObjectResult.getObjectUrl();
|
||||
|
||||
com.aliyun.oss.model.PutObjectResult objectResult = ossClient.putObject(aliyunProperty.getOSS().getBucketName(), objectKey, file.getInputStream());
|
||||
|
||||
if (!objectResult.getResponse().isSuccessful()){
|
||||
log.warn("上传文件失败 {}", objectMapper.writeValueAsString(objectResult));
|
||||
throw new BizException("上传文件失败 %s".formatted(objectResult.getResponse().getErrorResponseAsString()));
|
||||
|
||||
}else {
|
||||
AliyunProperty.OSSRegion ossRegion = aliyunProperty.getOSS().getRegions().stream().filter(x -> x.getCode().equalsIgnoreCase(aliyunProperty.getOSS().getRegion())).findFirst().orElseThrow(() -> new RuntimeException("region not found"));
|
||||
|
||||
objectUrl = "https://%s.%s/%s".formatted(aliyunProperty.getOSS().getBucketName(), ossRegion.getEndpoint(), objectKey);
|
||||
}
|
||||
|
||||
String objectUrl = putObjectResult.getObjectUrl();
|
||||
|
||||
|
||||
fileEntity.setPublicUrl(objectUrl);
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AliyunConfig {
|
||||
|
||||
@Autowired AliyunProperty aliyunProperty;
|
||||
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
@Lazy
|
||||
public OSSClient obsClient() {
|
||||
|
||||
AliyunProperty.OSSRegion ossRegion = aliyunProperty.getOSS().getRegions().stream().filter(x -> x.getCode().equalsIgnoreCase(aliyunProperty.getOSS().getRegion())).findFirst().orElseThrow(() -> new RuntimeException("region not found"));
|
||||
OSSClient ossClient = new OSSClient(ossRegion.getEndpoint(), aliyunProperty.getOSS().getAk(), aliyunProperty.getOSS().getSk());
|
||||
|
||||
return ossClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@ConfigurationProperties("aliyun")
|
||||
@Component
|
||||
@Data
|
||||
public class AliyunProperty {
|
||||
|
||||
private OSS OSS;
|
||||
|
||||
@Data
|
||||
public static class OSS {
|
||||
|
||||
private List<OSSRegion> regions;
|
||||
|
||||
private String ak;
|
||||
|
||||
private String sk;
|
||||
|
||||
private String region;
|
||||
|
||||
private String bucketName;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class OSSRegion{
|
||||
private String code;
|
||||
private String name;
|
||||
private String endpoint;
|
||||
private String internalEndpoint;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.AccessControlList;
|
||||
import com.obs.services.model.BucketTypeEnum;
|
||||
import com.obs.services.model.CreateBucketRequest;
|
||||
import com.obs.services.model.ObsBucket;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -30,14 +26,14 @@ public class HwCloudConfig {
|
||||
.orElseThrow(() -> new RuntimeException("zone not found"))
|
||||
.getEndpoint());
|
||||
|
||||
if (!client.headBucket(hwCloudProperty.getObs().getBucketName())) {
|
||||
CreateBucketRequest request = new CreateBucketRequest();
|
||||
request.setBucketName(hwCloudProperty.getObs().getBucketName());
|
||||
request.setBucketType(BucketTypeEnum.OBJECT);
|
||||
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
|
||||
ObsBucket bucket = client.createBucket(request);
|
||||
log.info("bucket created: {}", bucket.getBucketName());
|
||||
}
|
||||
// if (!client.headBucket(hwCloudProperty.getObs().getBucketName())) {
|
||||
// CreateBucketRequest request = new CreateBucketRequest();
|
||||
// request.setBucketName(hwCloudProperty.getObs().getBucketName());
|
||||
// request.setBucketType(BucketTypeEnum.OBJECT);
|
||||
// request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
|
||||
// ObsBucket bucket = client.createBucket(request);
|
||||
// log.info("bucket created: {}", bucket.getBucketName());
|
||||
// }
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -194,3 +194,134 @@ hwcloud:
|
||||
"code": "ap-southeast-1"
|
||||
"endpoint": "obs.ap-southeast-1.myhuaweicloud.com"
|
||||
|
||||
|
||||
# 用户登录名称 coal@1880219608457355.onaliyun.com
|
||||
#AccessKey ID LTAI5tQEJePBCd85weQT6z6Q
|
||||
#AccessKey Secret wjEePyMe6mvSsF1fCG3KG8IbxcYoag
|
||||
aliyun:
|
||||
oss:
|
||||
ak: "LTAI5tQEJePBCd85weQT6z6Q"
|
||||
sk: "wjEePyMe6mvSsF1fCG3KG8IbxcYoag"
|
||||
bucket-name: "licoal"
|
||||
region: "oss-cn-beijing"
|
||||
regions:
|
||||
- name: 华东1(杭州)
|
||||
code: oss-cn-hangzhou
|
||||
endpoint : oss-cn-hangzhou.aliyuncs.com
|
||||
internal-endpoint: oss-cn-hangzhou-internal.aliyuncs.com
|
||||
- name: 华东2(上海)
|
||||
code: oss-cn-shanghai
|
||||
endpoint : oss-cn-shanghai.aliyuncs.com
|
||||
internal-endpoint: oss-cn-shanghai-internal.aliyuncs.com
|
||||
- name: 华东5(南京-本地地域)
|
||||
code: oss-cn-nanjing
|
||||
endpoint : oss-cn-nanjing.aliyuncs.com
|
||||
internal-endpoint: oss-cn-nanjing-internal.aliyuncs.com
|
||||
- name: 华东6(福州-本地地域)
|
||||
code: oss-cn-fuzhou
|
||||
endpoint : oss-cn-fuzhou.aliyuncs.com
|
||||
internal-endpoint: oss-cn-fuzhou-internal.aliyuncs.com
|
||||
- name: 华中1(武汉-本地地域)
|
||||
code: oss-cn-wuhan
|
||||
endpoint : oss-cn-wuhan-lr.aliyuncs.com
|
||||
internal-endpoint: oss-cn-wuhan-lr-internal.aliyuncs.com
|
||||
- name: 华北1(青岛)
|
||||
code: oss-cn-qingdao
|
||||
endpoint : oss-cn-qingdao.aliyuncs.com
|
||||
internal-endpoint: oss-cn-qingdao-internal.aliyuncs.com
|
||||
- name: 华北2(北京)
|
||||
code: oss-cn-beijing
|
||||
endpoint : oss-cn-beijing.aliyuncs.com
|
||||
internal-endpoint: oss-cn-beijing-internal.aliyuncs.com
|
||||
- name: 华北 3(张家口)
|
||||
code: oss-cn-zhangjiakou
|
||||
endpoint : oss-cn-zhangjiakou.aliyuncs.com
|
||||
internal-endpoint: oss-cn-zhangjiakou-internal.aliyuncs.com
|
||||
- name: 华北5(呼和浩特)
|
||||
code: oss-cn-huhehaote
|
||||
endpoint : oss-cn-huhehaote.aliyuncs.com
|
||||
internal-endpoint: oss-cn-huhehaote-internal.aliyuncs.com
|
||||
- name: 华北6(乌兰察布)
|
||||
code: oss-cn-wulanchabu
|
||||
endpoint : oss-cn-wulanchabu.aliyuncs.com
|
||||
internal-endpoint: oss-cn-wulanchabu-internal.aliyuncs.com
|
||||
- name: 华南1(深圳)
|
||||
code: oss-cn-shenzhen
|
||||
endpoint : oss-cn-shenzhen.aliyuncs.com
|
||||
internal-endpoint: oss-cn-shenzhen-internal.aliyuncs.com
|
||||
- name: 华南2(河源)
|
||||
code: oss-cn-heyuan
|
||||
endpoint : oss-cn-heyuan.aliyuncs.com
|
||||
internal-endpoint: oss-cn-heyuan-internal.aliyuncs.com
|
||||
- name: 华南3(广州)
|
||||
code: oss-cn-guangzhou
|
||||
endpoint : oss-cn-guangzhou.aliyuncs.com
|
||||
internal-endpoint: oss-cn-guangzhou-internal.aliyuncs.com
|
||||
- name: 西南1(成都)
|
||||
code: oss-cn-chengdu
|
||||
endpoint : oss-cn-chengdu.aliyuncs.com
|
||||
internal-endpoint: oss-cn-chengdu-internal.aliyuncs.com
|
||||
- name: 中国香港
|
||||
code: oss-cn-hongkong
|
||||
endpoint : oss-cn-hongkong.aliyuncs.com
|
||||
internal-endpoint: oss-cn-hongkong-internal.aliyuncs.com
|
||||
- name: 美国(硅谷)
|
||||
code: oss-us-west-1
|
||||
endpoint : oss-us-west-1.aliyuncs.com
|
||||
internal-endpoint: oss-us-west-1-internal.aliyuncs.com
|
||||
- name: 美国(弗吉尼亚)
|
||||
code: oss-us-east-1
|
||||
endpoint : oss-us-east-1.aliyuncs.com
|
||||
internal-endpoint: oss-us-east-1-internal.aliyuncs.com
|
||||
- name: 日本(东京)
|
||||
code: oss-ap-northeast-1
|
||||
endpoint : oss-ap-northeast-1.aliyuncs.com
|
||||
internal-endpoint: oss-ap-northeast-1-internal.aliyuncs.com
|
||||
- name: 韩国(首尔)
|
||||
code: oss-ap-northeast-2
|
||||
endpoint : oss-ap-northeast-2.aliyuncs.com
|
||||
internal-endpoint: oss-ap-northeast-2-internal.aliyuncs.com
|
||||
- name: 新加坡
|
||||
code: oss-ap-southeast-1
|
||||
endpoint : oss-ap-southeast-1.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-1-internal.aliyuncs.com
|
||||
- name: 澳大利亚(悉尼)
|
||||
code: oss-ap-southeast-2
|
||||
endpoint : oss-ap-southeast-2.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-2-internal.aliyuncs.com
|
||||
- name: 马来西亚(吉隆坡)
|
||||
code: oss-ap-southeast-3
|
||||
endpoint : oss-ap-southeast-3.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-3-internal.aliyuncs.com
|
||||
- name: 印度尼西亚(雅加达)
|
||||
code: oss-ap-southeast-5
|
||||
endpoint : oss-ap-southeast-5.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-5-internal.aliyuncs.com
|
||||
- name: 菲律宾(马尼拉)
|
||||
code: oss-ap-southeast-6
|
||||
endpoint : oss-ap-southeast-6.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-6-internal.aliyuncs.com
|
||||
- name: 泰国(曼谷)
|
||||
code: oss-ap-southeast-7
|
||||
endpoint : oss-ap-southeast-7.aliyuncs.com
|
||||
internal-endpoint: oss-ap-southeast-7-internal.aliyuncs.com
|
||||
- name: 印度(孟买)
|
||||
code: oss-ap-south-1
|
||||
endpoint : oss-ap-south-1.aliyuncs.com
|
||||
internal-endpoint: oss-ap-south-1-internal.aliyuncs.com
|
||||
- name: 德国(法兰克福)
|
||||
code: oss-eu-central-1
|
||||
endpoint : oss-eu-central-1.aliyuncs.com
|
||||
internal-endpoint: oss-eu-central-1-internal.aliyuncs.com
|
||||
- name: 英国(伦敦)
|
||||
code: oss-eu-west-1
|
||||
endpoint : oss-eu-west-1.aliyuncs.com
|
||||
internal-endpoint: oss-eu-west-1-internal.aliyuncs.com
|
||||
- name: 阿联酋(迪拜)
|
||||
code: oss-me-east-1
|
||||
endpoint : oss-me-east-1.aliyuncs.com
|
||||
internal-endpoint: oss-me-east-1-internal.aliyuncs.com
|
||||
- name: 无地域属性(中国内地)
|
||||
code: oss-rg-china-mainland
|
||||
endpoint : oss-rg-china-mainland.aliyuncs.com
|
||||
internal-endpoint: 不支持
|
||||
|
||||
Reference in New Issue
Block a user