Skip to content

Instantly share code, notes, and snippets.

@flxxyz
Last active April 21, 2022 06:00
Show Gist options
  • Save flxxyz/b1328de7db8fe3f10418400348bac3cd to your computer and use it in GitHub Desktop.
Save flxxyz/b1328de7db8fe3f10418400348bac3cd to your computer and use it in GitHub Desktop.
OSS and VOD 上传文件
{
"type": "module",
"dependencies": {
"@alicloud/openapi-client": "^0.3.3",
"@alicloud/tea-typescript": "^1.7.1",
"@alicloud/vod20170321": "^2.0.1",
"ali-oss": "^6.15.0"
}
}
import OpenApi from "@alicloud/openapi-client";
import $vod20170321 from "@alicloud/vod20170321";
import * as $tea from "@alicloud/tea-typescript";
import OSS from "ali-oss";
const { default: vod20170321 } = $vod20170321;
export class OSSClient {
static ACCELERATE_ENDPOINT = "oss-accelerate.aliyuncs.com";
constructor(accessKeyId, accessKeySecret, options = {}) {
const { stsToken, endpoint = OSSClient.ACCELERATE_ENDPOINT, bucket, region } = options;
const token = {
accessKeyId,
accessKeySecret,
};
if (stsToken) {
token.stsToken = stsToken;
}
this.$store = new OSS({
...token,
refreshSTSToken: async () => token,
secure: true,
endpoint,
bucket,
region,
});
}
async upload(filename, filepath) {
let resp = null;
try {
resp = await this.$store.put(filename, filepath, {});
} catch (err) {
throw $tea.newError(err);
}
return resp;
}
}
export class VodClient {
static ENDPOINT = "vod.cn-beijing.aliyuncs.com";
static USER_DATA = {
Type: "oss",
Domain: OSSClient.ACCELERATE_ENDPOINT,
};
constructor(accessKeyId, accessKeySecret, options = {}) {
const { endpoint = VodClient.ENDPOINT } = options;
let config = new OpenApi.Config({
accessKeyId,
accessKeySecret,
endpoint,
});
this.$vod = new vod20170321(config);
}
async _create(fileName, title, options) {
const { coverURL, workflowId, userData } = options;
const opts = {
fileName,
title,
userData: JSON.stringify(userData),
};
if (coverURL) {
opts.coverURL = coverURL;
}
if (workflowId) {
opts.workflowId = workflowId;
}
let createUploadVideoRequest = new $vod20170321.CreateUploadVideoRequest(opts);
let resp = await this.$vod.createUploadVideo(createUploadVideoRequest);
resp = $tea.toMap(resp);
resp.body.UploadAddress = JSON.parse(Buffer.from(resp.body.UploadAddress, "base64").toString("ascii")) || {};
resp.body.UploadAuth = JSON.parse(Buffer.from(resp.body.UploadAuth, "base64").toString("ascii")) || {};
return resp;
}
async upload(options = {}) {
const {
filename,
filepath,
coverURL = '',
title,
workflowId = '',
userData = VodClient.USER_DATA,
type,
} = options;
const resp = await this._create(filename, title, {
coverURL,
workflowId,
userData
});
const { Endpoint: DO_NOT_USE, Bucket, FileName } = resp.body.UploadAddress;
const { AccessKeyId, AccessKeySecret, SecurityToken, Region, Expiration } = resp.body.UploadAuth;
const store = new OSSClient(
AccessKeyId,
AccessKeySecret,
{
stsToken: SecurityToken,
endpoint: OSSClient.ACCELERATE_ENDPOINT,
bucket: Bucket,
region: Region,
},
);
// TODO: vod上传待验证
if (type) {
return await store.$store.signatureUrl(filename, {
"expires": Number(Expiration),
"method": "PUT",
"Content-Type": type
});
}
return await store.upload(FileName, filepath);
}
}
import { VodClient, OSSClient } from "./vod_and_oss_to_upload.js";
/**
* 生成随机字符串
* @param {Number} length
* @returns
*/
function randomString(length) {
const str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = [];
for (let i = length; i > 0; --i) {
result.push(str[Math.floor(Math.random() * str.length)]);
}
return result.join('');
}
/**
* 视频上传
* @param {String} accessKeyId
* @param {String} accessKeySecret
* @param {Object} options
* @returns
*/
async function vodUpload(accessKeyId, accessKeySecret, options = {}) {
const { filename, title = '', filepath, coverURL = '', workflowId, type } = options;
const vod = new VodClient(accessKeyId, accessKeySecret);
return await vod.upload({
filename,
filepath,
title,
coverURL,
workflowId,
type,
});
}
/**
* 封面上传
* @param {String} accessKeyId
* @param {String} accessKeySecret
* @param {Object} options
* @returns
*/
async function coverUpload(accessKeyId, accessKeySecret, options = {}) {
const { filename, filepath, bucket, region } = options;
const store = new OSSClient(
accessKeyId,
accessKeySecret,
{
bucket,
region,
});
return await store.upload(filename, filepath);
}
const coverName = "test_cover.jpg";
const coverPath = "/Users/longaotian/Desktop/test_cover.jpg";
const coverUploadOptions = {
filename: coverName,
filepath: coverPath,
bucket: "Your Bucket Name",
region: "oss-cn-qingdao",
};
const coverResp = await coverUpload("Your AccessKeyId", "Your AccessKeySecret", coverUploadOptions);
console.log("----------- coverUpload() resp:", coverResp);
const videoName = "test_video.mp4";
const videoTitle = randomString(16);
console.log("----------- videoTitle:", videoTitle);
const videoPath = "/Users/longaotian/Desktop/test_video.mp4";
const vodUploadOptions = {
filename: videoName,
title: videoTitle,
filepath: videoPath,
coverURL: coverResp.url,
// workflowId: "Your WorkflowId",
// type: "video/mp4",
};
const vodResp = await vodUpload("Your AccessKeyId", "Your AccessKeySecret", vodUploadOptions);
console.log("----------- vodUpload() resp:", vodResp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment