|
@@ -0,0 +1,80 @@
|
|
|
|
|
+/*
|
|
|
|
|
+ * Copyright (c) 2018-2025, hnqz All rights reserved.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Redistribution and use in source and binary forms, with or without
|
|
|
|
|
+ * modification, are permitted provided that the following conditions are met:
|
|
|
|
|
+ *
|
|
|
|
|
+ * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
+ * this list of conditions and the following disclaimer.
|
|
|
|
|
+ * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
+ * notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
+ * documentation and/or other materials provided with the distribution.
|
|
|
|
|
+ * Neither the name of the pig4cloud.com developer nor the names of its
|
|
|
|
|
+ * contributors may be used to endorse or promote products derived from
|
|
|
|
|
+ * this software without specific prior written permission.
|
|
|
|
|
+ * Author: hnqz
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+package net.yyc.common.oss.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
|
+import com.aliyun.oss.model.GeneratePresignedUrlRequest;
|
|
|
|
|
+import net.yyc.common.oss.OssProperties2;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 阿里云 OSS V1 签名实现(基于 oss2 配置,生成长有效期 URL)
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>V1 签名不强制 7 天上限,expiresSeconds 可传入任意正数(如一年)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author hnqz
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class AliyunOssSigner implements OssSigner {
|
|
|
|
|
+
|
|
|
|
|
+ private final OssProperties2 properties;
|
|
|
|
|
+
|
|
|
|
|
+ private OSS ossClient;
|
|
|
|
|
+
|
|
|
|
|
+ public AliyunOssSigner(OssProperties2 properties) {
|
|
|
|
|
+ this.properties = properties;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PostConstruct
|
|
|
|
|
+ public void init() {
|
|
|
|
|
+ log.info("初始化阿里云 OSS V1 签名客户端, endpoint={}, bucket={}, objectKeyPre={}",
|
|
|
|
|
+ properties.getEndpoint(), properties.getBucketName(), properties.getObjectKeyPre());
|
|
|
|
|
+ this.ossClient = new OSSClientBuilder().build(
|
|
|
|
|
+ properties.getEndpoint(),
|
|
|
|
|
+ properties.getAccessKey(),
|
|
|
|
|
+ properties.getSecretKey());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String sign(String objectKey, long expiresSeconds) {
|
|
|
|
|
+ String prefix = properties.getObjectKeyPre() != null ? properties.getObjectKeyPre() : "";
|
|
|
|
|
+ return sign(properties.getBucketName(), prefix + objectKey, expiresSeconds);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String sign(String bucket, String objectKey, long expiresSeconds) {
|
|
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + expiresSeconds * 1000L);
|
|
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, objectKey);
|
|
|
|
|
+ request.setExpiration(expiration);
|
|
|
|
|
+ return ossClient.generatePresignedUrl(request).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @PreDestroy
|
|
|
|
|
+ public void destroy() {
|
|
|
|
|
+ if (ossClient != null) {
|
|
|
|
|
+ ossClient.shutdown();
|
|
|
|
|
+ log.info("阿里云 OSS 客户端已关闭");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|