UNPKG

vodpro-upload-js-sdk

Version:

Tencent Cloud VOD Pro Upload SDK

244 lines (192 loc) 7.64 kB
## 注意事项 由 core1发起的上传,无法使用 core2来访问和操作 const core1 = new Core({ ... }); const core2 = new Core({ ... }); taskId由core1返回 core2.pauseTask(taskId); // 暂停无效,core2实例无法操作 core1的队列 ## 用法 ### 前置条件:客户开通云点播专业版应用及对应地域存储桶。 ### 服务端 Java Demo ``` import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.vod.v20240718.VodClient; import com.tencentcloudapi.vod.v20240718.models.CreateStorageCredentialsRequest; import com.tencentcloudapi.vod.v20240718.models.CreateStorageCredentialsResponse; import org.json.JSONArray; import org.json.JSONObject; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; /** * 凭证辅助类,负责获取临时存储凭证 */ public class CredentialHelper { // 常量定义 private static final long APP_ID = 251000000; // 腾讯云账号 APPID private static final long SUB_APP_ID = 1234567890; // 云点播专业版应用 APPID private static final String BUCKET_ID = "bucketid1"; // 云点播专业版应用存储桶的 ID private static final String FILE_KEY = "upload/demo.mp4"; // 上传到存储后的文件 KEY private static final String REGION = "ap-guangzhou"; // 存储桶所在地域 /** * 凭证对象,存储临时凭证信息 */ public static class Cred { private final String accessKeyId; private final String secretAccessKey; private final String sessionToken; public Cred(String accessKeyId, String secretAccessKey, String sessionToken) { this.accessKeyId = accessKeyId; this.secretAccessKey = secretAccessKey; this.sessionToken = sessionToken; } public String getAccessKeyId() { return accessKeyId; } public String getSecretAccessKey() { return secretAccessKey; } public String getSessionToken() { return sessionToken; } } /** * 获取临时凭证 * * @return 临时凭证对象 * @throws Exception 如果获取凭证失败 */ public static Cred getCredential() throws Exception { try { // 1. 初始化腾讯云 API 客户端 Credential credential = new Credential("SecretId", "SecretKey"); // 腾讯云账号 SecretId 和 SecretKey ClientProfile clientProfile = new ClientProfile(); // 客户端配置 VodClient vodClient = new VodClient(credential, "ap-guangzhou", clientProfile); // 创建 VodClient 对象 // 2. 构造并编码策略 String policyJson = createPolicyJson(); String encodedPolicy = URLEncoder.encode(policyJson, StandardCharsets.UTF_8.name()); // 3. 创建并发送请求 CreateStorageCredentialsRequest req = new CreateStorageCredentialsRequest(); req.setSubAppId(SUB_APP_ID); // 云点播专业版应用 APPID req.setPolicy(encodedPolicy); // 策略 // 4. 获取响应并返回凭证 CreateStorageCredentialsResponse resp = vodClient.CreateStorageCredentials(req); System.out.println("获取存储凭证成功: " + resp); return new Cred( resp.getCredentials().getAccessKeyId(), resp.getCredentials().getSecretAccessKey(), resp.getCredentials().getSessionToken()); } catch (TencentCloudSDKException e) { System.err.println("获取存储凭证失败: " + e.getMessage()); throw new Exception("获取存储凭证失败", e); } } /** * 创建策略JSON字符串,使用 org.json 库 * * @return 策略JSON字符串 */ private static String createPolicyJson() { // 构建资源路径 String resource = String.format( "qcs::vod:%s:uid/%d:prefix//%d/%s/%s", REGION, APP_ID, SUB_APP_ID, BUCKET_ID, FILE_KEY); // 构建操作列表 String[] actions = { "name/vod:PutObject", "name/vod:ListParts", "name/vod:PostObject", "name/vod:CreateMultipartUpload", "name/vod:UploadPart", "name/vod:CompleteMultipartUpload", "name/vod:AbortMultipartUpload", "name/vod:ListMultipartUploads" }; // 使用 JSONObject 构建 JSON JSONObject policy = new JSONObject(); policy.put("version", "2.0"); JSONArray statements = new JSONArray(); JSONObject statement = new JSONObject(); JSONArray actionArray = new JSONArray(); for (String action : actions) { actionArray.put(action); } statement.put("action", actionArray); statement.put("effect", "allow"); JSONArray resources = new JSONArray(); resources.put(resource); statement.put("resource", resources); statements.put(statement); policy.put("statement", statements); return policy.toString(); } } ``` ### pom.xml ``` <repositories> <repository> <id>tencentcloud-repo</id> <name>Tencent Cloud Repository</name> <url>https://mirrors.tencent.com/nexus/repository/maven-public/</url> </repository> </repositories> <dependencies> <!-- AWS SDK for Java 2.0 --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> <version>${aws.sdk.version}</version> </dependency> <!-- Replace Apache HTTP client with Netty for async operations --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>netty-nio-client</artifactId> <version>${aws.sdk.version}</version> </dependency> <!-- S3 Transfer Manager --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3-transfer-manager</artifactId> <version>${aws.sdk.version}</version> </dependency> <!-- SDK Core --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sdk-core</artifactId> <version>${aws.sdk.version}</version> </dependency> <!-- Checksums --> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>checksums</artifactId> <version>${aws.sdk.version}</version> </dependency> <!-- Tencent Cloud SDK --> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java-vod</artifactId> <version>${tencentcloud.version}</version> </dependency> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java-common</artifactId> <version>${tencentcloud.version}</version> </dependency> <!-- JSON Library --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20230227</version> </dependency> </dependencies> ``` ### 前端上传示例请参考路径demo下的演示 ``` npm i nmm run start ```