阿里云oss简单获取视频第一帧工具类

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();
    }
}
相关推荐
0xCC说逆向1 小时前
Windows图形界面(GUI)-QT-C/C++ - QT 窗口属性
c语言·开发语言·c++·windows·qt·mfc
九河云1 小时前
腾讯CDN vs 阿里云CDN:哪个更适合你的业务?
数据库·阿里云·云计算
抠脚学代码1 小时前
ESP8266 MQTT服务器+阿里云
mqtt·阿里云·esp8266
算家云4 小时前
LatentSync本地部署教程:基于音频精准生成唇形高度同步视频
人工智能·音视频·模型部署·字节跳动·算家云·latentsync
折途4 小时前
I2S是什么通信协议?它如何传输音频数据?它和I2C是什么关系?
单片机·嵌入式硬件·物联网·音视频
山海里啊有星辰4 小时前
Video-RAG:一种将视频RAG新框架
音视频
^@^lemon tea^@^5 小时前
WPF 引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常 解决办法
windows·wpf·ocx控件开发错误
hmywillstronger6 小时前
【Grasshopper】【Python】点集排序:带索引的Z字形排序算法
windows·python·排序算法
无限大.6 小时前
使用 Flask 构建视频转 GIF 工具
python·flask·音视频