集成阿里云OSS编写AliyunOSSUtil
我们通过调用官方SDK方式集成 阿里云SDK:https://help.aliyun.com/document_detail/32008.htm?spm=a2c4g.11186623.2.6.5032f2eekNXgW7#concept-32008-zh
1、引入sdk依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
2、添加yml配置文件
oss:
bucket: 你的bucket
endPoint: https://oss-cn-shenzhen.aliyuncs.com
accessKeyId: 你的 accessKeyId
accessKeySecret: 你的 accessKeySecret
3、编写 AliyunOSSUtil文件
3.1 多例模式
package com.mtl.aliyunoss.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URL;
import java.util.Calendar;
@Component
public class AliyunOSSUtil {
private static String BUCKET_NAME;
private static String KEY_ID;
private static String KEY_SECRET;
private static String END_POINT;
private static final int URL_EXPIRATION = 365 * 10;
@Value("${oss.bucket}")
private String bucketName;
@Value("${oss.endPoint}")
private String accessEndPoint;
@Value("${oss.accessKeyId}")
private String accessKeyId;
@Value("${oss.accessKeySecret}")
private String accessKeySecret;
@PostConstruct
public void init() {
BUCKET_NAME = bucketName;
END_POINT = accessEndPoint;
KEY_ID = accessKeyId;
KEY_SECRET = accessKeySecret;
}
private static OSS getOSSClient() {
OSS ossClient = new OSSClientBuilder().build(END_POINT, KEY_ID, KEY_SECRET);
System.out.println(ossClient);
return ossClient;
}
public static void uploadFile(String allPathFile, InputStream inputStream) {
OSS ossClient = getOSSClient();
try {
ossClient.putObject(BUCKET_NAME, allPathFile, inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
depoly(ossClient);
}
}
public static void uploadFile(String allPathFile, File file) {
try {
uploadFile(allPathFile, new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static String getVisitUrl(String allPathFile) {
OSS ossClient = getOSSClient();
try {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, URL_EXPIRATION);
URL url = ossClient.generatePresignedUrl(BUCKET_NAME, allPathFile, calendar.getTime());
if (url != null) {
return url.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
depoly(ossClient);
}
return null;
}
private static void depoly(OSS ossClient) {
try {
if (ossClient != null) {
ossClient.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.2 单例模式
3.2.1 静态内部类-延时加载
- 替换 多例 getOSSClient 方法;
- 加上 SingletonOSS 内部类静态方法
private static class SingletonOSS {
private final static OSS ossClient = new OSSClientBuilder().build(END_POINT, KEY_ID, KEY_SECRET);
}
public static OSS getOSSClient() {
return SingletonOSS.ossClient;
}
3.2.2 枚举-延时加载
- 替换 多例 getOSSClient 方法;
- 加上 SingletonOSS 内部类静态方法
public enum OSSEnumSignletion {
INSTANCE;
private OSS ossClient;
OSSEnumSignletion() {
ossClient = new OSSClientBuilder().build(END_POINT, KEY_ID, KEY_SECRET);
}
public OSS ossClient() {
return ossClient;
}
}
public static OSS getOSSClient() {
return OSSEnumSignletion.INSTANCE.ossClient();
}
4、单元测试调用
@SpringBootTest
class AliyunossApplicationTests {
@Test
void contextLoads() throws InterruptedException {
long start = System.currentTimeMillis();
int threadNum = 10;
final CountDownLatch countDownLatch = new CountDownLatch(threadNum);
while (threadNum > 0) {
new Thread(() -> {
System.out.println("---------------线程:" + Thread.currentThread());
for (int j = 0; j < 10; j++) {
AliyunOSSUtil.uploadFile("111/1.png", new File("C:\\Users\\mtl\\Desktop\\1.png"));
String visitUrl = AliyunOSSUtil.getVisitUrl("111/1.png");
System.out.println(visitUrl);
}
countDownLatch.countDown();
}).start();
threadNum--;
}
countDownLatch.await();
long end = System.currentTimeMillis();
System.out.println("总耗时:" + (end - start) + "ms");
}
}
|