文件上传 阿里云
parent
7f200a5758
commit
fa1d6029c4
|
|
@ -33,6 +33,11 @@
|
|||
<artifactId>minio</artifactId>
|
||||
<version>8.5.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.17.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.njzscloud.common.minio.config;
|
||||
|
||||
import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import com.aliyun.oss.common.comm.SignVersion;
|
||||
import com.njzscloud.common.minio.controller.OSSController;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(MinioProperties.class)
|
||||
public class AliOSSAutoConfiguration {
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
public OSS ossClient(MinioProperties minioProperties) {
|
||||
String endpoint = minioProperties.getEndpoint();
|
||||
String accessKey = minioProperties.getAccessKey();
|
||||
String secretKey = minioProperties.getSecretKey();
|
||||
String region = minioProperties.getRegion();
|
||||
|
||||
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
||||
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
||||
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(accessKey, secretKey);
|
||||
return OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.region(region)
|
||||
.credentialsProvider(credentialProvider)
|
||||
.clientConfiguration(clientBuilderConfiguration)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OSSController ossController() {
|
||||
return new OSSController();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,14 @@ package com.njzscloud.common.minio.config;
|
|||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties("oss.minio")
|
||||
public class MinioProperties {
|
||||
private String endpoint;
|
||||
private String region;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucketName;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil;
|
|||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njzscloud.common.core.tuple.Tuple2;
|
||||
import com.njzscloud.common.core.utils.R;
|
||||
import com.njzscloud.common.minio.util.AliOSS;
|
||||
import com.njzscloud.common.minio.util.Minio;
|
||||
import com.njzscloud.common.mvc.util.FileResponseUtil;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
|
@ -33,14 +34,12 @@ public class OSSController {
|
|||
}
|
||||
|
||||
@GetMapping("/obtain_presigned_url")
|
||||
public R<Map<String, String>> obtainPresignedUrl(
|
||||
@RequestParam(value = "bucketName") String bucketName,
|
||||
@RequestParam(value = "filename") String filename) {
|
||||
public R<Map<String, String>> obtainPresignedUrl(@RequestParam(value = "filename") String filename) {
|
||||
String objectName = IdUtil.fastSimpleUUID();
|
||||
if (StrUtil.isNotBlank(filename)) {
|
||||
objectName = objectName + "." + FileUtil.extName(filename);
|
||||
}
|
||||
return R.success(Minio.obtainPresignedUrl(bucketName, objectName));
|
||||
return R.success(AliOSS.obtainPresignedUrl(objectName));
|
||||
}
|
||||
|
||||
@GetMapping("/download/{bucketName}/{objectName}")
|
||||
|
|
@ -48,7 +47,7 @@ public class OSSController {
|
|||
@PathVariable("bucketName") String bucketName,
|
||||
@PathVariable("objectName") String objectName,
|
||||
HttpServletResponse response) {
|
||||
Tuple2<InputStream, String> tuple2 = Minio.obtainFile(bucketName, objectName);
|
||||
Tuple2<InputStream, String> tuple2 = AliOSS.obtainFile(bucketName, objectName);
|
||||
FileResponseUtil.download(response, tuple2.get_0(), tuple2.get_1(), objectName);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,118 @@
|
|||
package com.njzscloud.common.minio.util;
|
||||
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.common.utils.BinaryUtil;
|
||||
import com.aliyun.oss.model.GetObjectRequest;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PolicyConditions;
|
||||
import com.njzscloud.common.core.tuple.Tuple2;
|
||||
import com.njzscloud.common.minio.config.MinioProperties;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public final class AliOSS {
|
||||
private static final OSS CLIENT;
|
||||
private static final String BUCKET_NAME;
|
||||
private static final String ACCESS_KEY;
|
||||
|
||||
static {
|
||||
CLIENT = SpringUtil.getBean(OSS.class);
|
||||
BUCKET_NAME = SpringUtil.getBean(MinioProperties.class).getBucketName();
|
||||
ACCESS_KEY = SpringUtil.getBean(MinioProperties.class).getAccessKey();
|
||||
if (StrUtil.isNotBlank(BUCKET_NAME)) {
|
||||
createBucket(BUCKET_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
public static void createBucket(String bucketName) {
|
||||
Assert.notBlank(bucketName, "未指明存储位置");
|
||||
try {
|
||||
boolean exists = CLIENT.doesBucketExist(bucketName);
|
||||
if (exists) return;
|
||||
CLIENT.createBucket(bucketName);
|
||||
} catch (Exception e) {
|
||||
log.error("存储桶创建失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> obtainPresignedUrl(String objectName) {
|
||||
Map<String, String> data = new HashMap<>();
|
||||
try {
|
||||
long expireEndTime = System.currentTimeMillis() + 3600 * 1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
PolicyConditions policyConds = new PolicyConditions();
|
||||
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
|
||||
String postPolicy = CLIENT.generatePostPolicy(expiration, policyConds);
|
||||
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
|
||||
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
||||
String postSignature = CLIENT.calculatePostSignature(postPolicy);
|
||||
data.put("OSSAccessKeyId", ACCESS_KEY);
|
||||
data.put("policy", encodedPolicy);
|
||||
data.put("signature", postSignature);
|
||||
data.put("success_action_status", "200");
|
||||
data.put("name", objectName);
|
||||
data.put("key", objectName);
|
||||
data.put("bucketName", BUCKET_NAME);
|
||||
data.put("objectName", objectName);
|
||||
} catch (
|
||||
OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
// 假设此方法存在
|
||||
System.out.println("HTTP Status Code: " + oe.getRawResponseError());
|
||||
System.out.println("Error Message: " + oe.getErrorMessage());
|
||||
System.out.println("Error Code: " + oe.getErrorCode());
|
||||
System.out.println("Request ID: " + oe.getRequestId());
|
||||
System.out.println("Host ID: " + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message: " + ce.getMessage());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public static Tuple2<InputStream, String> obtainFile(String bucketName, String objectName) {
|
||||
try {
|
||||
// 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
|
||||
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
|
||||
OSSObject response = CLIENT.getObject(new GetObjectRequest(bucketName, objectName));
|
||||
ObjectMetadata objectMetadata = response.getObjectMetadata();
|
||||
String contentType = objectMetadata.getContentType();
|
||||
InputStream objectContent = response.getObjectContent();
|
||||
return Tuple2.create(objectContent, contentType);
|
||||
|
||||
} catch (OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
System.out.println("Error Message:" + oe.getErrorMessage());
|
||||
System.out.println("Error Code:" + oe.getErrorCode());
|
||||
System.out.println("Request ID:" + oe.getRequestId());
|
||||
System.out.println("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
|
||||
com.njzscloud.common.minio.config.MinioAutoConfiguration
|
||||
com.njzscloud.common.minio.config.AliOSSAutoConfiguration
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ public final class Constants {
|
|||
|
||||
public static final String ROLE_AUTHENTICATED = "ROLE_AUTHENTICATED";
|
||||
public static final String ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
|
||||
public static final String ROLE_ADMIN = "ROLE_ADMIN";
|
||||
|
||||
// Redis 订阅频道 权限更新
|
||||
public static final String REDIS_TOPIC_PERMISSION_UPDATE = "permission_update";
|
||||
|
|
|
|||
|
|
@ -24,6 +24,26 @@ public class SecurityUtil {
|
|||
return userDetail == null ? Constants.ANONYMOUS_USER : userDetail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是管理员
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
public static boolean isAdmin() {
|
||||
UserDetail userDetail = SecurityUtil.loginUser();
|
||||
return userDetail.getRoles().contains(Constants.ROLE_ADMIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户 ID
|
||||
*
|
||||
* @return 用户 ID
|
||||
*/
|
||||
public static Long currentUserId() {
|
||||
UserDetail userDetail = SecurityUtil.loginUser();
|
||||
return userDetail.getUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户信息和 TOKEN
|
||||
*
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ public class TempStoragePointService extends ServiceImpl<TempStoragePointMapper,
|
|||
*/
|
||||
public PageResult<TempStoragePointEntity> paging(PageParam pageParam, TempStoragePointEntity tempStoragePointEntity) {
|
||||
UserDetail userDetail = SecurityUtil.loginUser();
|
||||
boolean isAdmin = userDetail.getRoles().contains("admin");
|
||||
boolean isAdmin = userDetail.getRoles().contains("ROLE_ADMIN");
|
||||
Long userId = userDetail.getUserId();
|
||||
String pointName = tempStoragePointEntity.getPointName();
|
||||
PointStatus status = tempStoragePointEntity.getStatus();
|
||||
|
|
|
|||
|
|
@ -25,10 +25,11 @@ spring:
|
|||
oss:
|
||||
path: D:/ProJects/gov_manage/njzscloud-supervisory-svr/logs
|
||||
minio:
|
||||
endpoint: http://localhost:9000
|
||||
access-key: minioadmin
|
||||
secret-key: minioadmin
|
||||
bucket-name: zsy
|
||||
endpoint: oss-cn-shanghai.aliyuncs.com
|
||||
access-key: LTAI5tJJu2WayYchExrT5W1E
|
||||
secret-key: zllX0ZJ1EwsZXT6dE6swCLgTF4ImGg
|
||||
region: cn-shanghai
|
||||
bucket-name: cdn-zsy
|
||||
|
||||
app:
|
||||
district:
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
FROM dispose_record
|
||||
WHERE deleted = 0
|
||||
AND DATE_FORMAT(out_time, '%Y-%m') = '${date}'
|
||||
GROUP BY DATE_FORMAT(out_time, '%Y-%m-%d'), garbage_category
|
||||
GROUP BY CAST(DATE_FORMAT(out_time, '%d') AS SIGNED), garbage_category
|
||||
ORDER BY CAST(DATE_FORMAT(out_time, '%d') AS SIGNED) DESC, garbage_category
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.njzscloud.supervisory.menu.mapper.SysMenuMapper">
|
||||
<select id="selectMenuByUserId" resultType="com.njzscloud.supervisory.menu.pojo.SysMenuEntity">
|
||||
SELECT a.id,
|
||||
SELECT DISTINCT a.id,
|
||||
a.sn,
|
||||
a.pid,
|
||||
a.title,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<result property="breadcrumb" column="breadcrumb" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
|
||||
</resultMap>
|
||||
<select id="listUserMenu" resultMap="listUserMenuMap">
|
||||
SELECT a.id,
|
||||
SELECT DISTINCT a.id,
|
||||
a.sn,
|
||||
a.pid,
|
||||
a.title,
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
</select>
|
||||
|
||||
<select id="listUserEndpoint" resultType="com.njzscloud.common.security.support.EndpointResource">
|
||||
SELECT a.id,
|
||||
SELECT DISTINCT a.id,
|
||||
a.request_method,
|
||||
a.routing_path,
|
||||
a.endpoint_path,
|
||||
|
|
|
|||
|
|
@ -86,6 +86,7 @@
|
|||
a.microdistrict,
|
||||
a.property_management,
|
||||
a.video_url,
|
||||
a.manager_id,
|
||||
a.creator_id,
|
||||
a.modifier_id,
|
||||
a.create_time,
|
||||
|
|
|
|||
|
|
@ -182,3 +182,9 @@ Content-Type: application/json
|
|||
"carOutFront": "https://cdn-zsy.oss-cn-shanghai.aliyuncs.com/car-demo3.png",
|
||||
"carOutBody": "https://cdn-zsy.oss-cn-shanghai.aliyuncs.com/car-demo3.png"
|
||||
}
|
||||
|
||||
### 上传文件
|
||||
PUT http://cdn-zsy.oss-cn-shanghai.aliyuncs.com/91e40d82e142474694ee5bb12829e1b8.jpg?x-oss-date=20250826T055256Z&x-oss-expires=3599&x-oss-signature-version=OSS4-HMAC-SHA256&x-oss-credential=LTAI5tJJu2WayYchExrT5W1E%2F20250826%2Fcn-shanghai%2Foss%2Faliyun_v4_request&x-oss-signature=fb63afba4911d9438ea78ad42ce985dda6c473b57541b2e9ecf38a3507fa101a
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< C:\Users\24955\Pictures\bizhihui_com_20231111130802169967928244585.jpg
|
||||
|
|
|
|||
Loading…
Reference in New Issue