java
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.File;
import java.io.InputStream;
import java.util.List;
/**
* @ClassName: OssJavaSdkQuickStart
* @Description:
* @Author: 张小辉
* @Date: 2025-01-16
* @Version: 1.0
**/
public class OSSUtil {
// 阿里云 OSS 配置信息
private static final String ENDPOINT = "https://oss-cn-wulanchabu.aliyuncs.com"; // OSS 区域地址
private static final String ACCESS_KEY_ID = "LTAI5tGBHLz1bdqs8ZFbrPYf"; // AccessKey ID
private static final String ACCESS_KEY_SECRET = "Ukqv5d4XvOyHyX5QBEznLdsSwMSHhv"; // AccessKey Secret
private static final String BUCKET_NAME = "grouptrain"; // Bucket 名称
private static OSS ossClient;
// 初始化 OSS 客户端
private static OSS getOSSClient() {
if (ossClient == null) {
ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
}
return ossClient;
}
// 关闭 OSS 客户端
public static void shutdown() {
if (ossClient != null) {
ossClient.shutdown();
}
}
// 上传文件
public static void uploadFile(String objectName, File file) {
try {
OSS ossClient = getOSSClient();
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, file);
ossClient.putObject(putObjectRequest);
System.out.println("文件上传成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 上传文件流
public static void uploadFile(String objectName, InputStream inputStream) {
try {
OSS ossClient = getOSSClient();
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, inputStream);
ossClient.putObject(putObjectRequest);
System.out.println("文件上传成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 下载文件
public static void downloadFile(String objectName, String downloadFilePath) {
try {
OSS ossClient = getOSSClient();
ossClient.getObject(new GetObjectRequest(BUCKET_NAME, objectName), new File(downloadFilePath));
System.out.println("文件下载成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 删除文件
public static void deleteFile(String objectName) {
try {
OSS ossClient = getOSSClient();
ossClient.deleteObject(BUCKET_NAME, objectName);
System.out.println("文件删除成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 列出文件
public static void listFiles() {
try {
OSS ossClient = getOSSClient();
ObjectListing objectListing = ossClient.listObjects(BUCKET_NAME);
List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
for (OSSObjectSummary summary : objectSummaries) {
System.out.println("Object: " + summary.getKey() + ", Size: " + summary.getSize());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取视频第一帧截图 URL
* @param videoObjectKey:视频文件的 Object Key-视频路径
*/
public static void getVideoSnapshotUrl(String videoObjectKey) {
// 创建 OSS 客户端实例
OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
try {
// 构造视频截图 URL
String snapshotUrl = String.format(
"https://%s.%s/%s?x-oss-process=video/snapshot,t_0,f_jpg",
BUCKET_NAME, ENDPOINT.replace("https://", ""), videoObjectKey
);
System.out.println("视频第一帧截图 URL:" + snapshotUrl);
// 可以将 snapshotUrl 直接用于下载或前端显示
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭 OSS 客户端
ossClient.shutdown();
}
}
public static void main(String[] args) {
// 示例:上传文件
// File file = new File("/Users/engine/Desktop/WechatIMG1210.jpg");
// uploadFile("WechatIMG1210.jpg", file);
//
示例:下载文件
// downloadFile("DATA.json", "/Users/engine/Downloads/DATA.json");
//
// // 示例:删除文件
// deleteFile("WechatIMG1210.jpg");
//
// // 示例:列出文件
// listFiles();
//
// 示例:获取视频第一帧截图 URL
getVideoSnapshotUrl("21.mp4");
// 关闭客户端
shutdown();
}
}