c#对接阿里云视频转码服务之删除

先讲删除,上传和接收转码之后讲。

首先阿里云的视频转码服务的源文件和转码后的文件以及视频的封面会存储在阿里云的对象存储OSS里,我的做法是分两个Bucket,一个用来存放源文件,一个用来存放转码后的文件以及视频封面。所以我们在删除的时候要全部删除。

C#直接引用阿里云的Aliyun.OSS.dll,引用好就可以开刷删除了。

        /// <summary>
        /// 这里自行配置相关属性
        /// </summary>
        public string AccessKeyId
        {
            get
            {
                return ConfigurationManager.AppSettings["AccessKeyId"];
            }
            set
            {
                AccessKeyId = value;
            }
        }
        public string AccessKeySecret
        {
            get
            {
                return ConfigurationManager.AppSettings["AccessKeySecret"];
            }
            set
            {
                AccessKeySecret = value;
            }
        }
        public string Endpoint
        {
            get
            {
                return ConfigurationManager.AppSettings["Endpoint"];
            }
            set
            {
                Endpoint = value;
            }
        }
        public string InputBucketName
        {
            get
            {
                return ConfigurationManager.AppSettings["InputBucketName"];
            }
            set
            {
                InputBucketName = value;
            }
        }
        public string OutputBucketName
        {
            get
            {
                return ConfigurationManager.AppSettings["OutputBucketName"];
            }
            set
            {
                OutputBucketName = value;
            }
        }
        /// <summary>
        /// 视频输出输入共同放目录 
        /// </summary>
        public string Folder
        {
            get
            {
                return ConfigurationManager.AppSettings["folder"];
            }
            set
            {
                Folder = value;
            }
        }
        /// <summary>
        /// 视频图片存放目录
        /// </summary>
        public string ImgFolder
        {
            get
            {
                return ConfigurationManager.AppSettings["ImgFolder"];
            }
            set
            {
                ImgFolder = value;
            }
        }
    }

        /// <summary>
        /// 删除OSS相关文件
        /// </summary>
        /// <param name="RunId">文件的标识,阿里云提供的</param>
        /// <param name="FileName">文件名</param>
        /// <param name="FileType">文件的后缀</param>
        /// <param name="ossConfig">存放一系列的相关参数</param>
        /// <param name="client">OssClient实例</param>
        /// <param name="delType">1.删除输入相关文件 | 2.删除输入源文件|3.删除视频封面图片</param>
        public void DelOssFile(string RunId, string FileName, string FileType, NiurenOSSConfig ossConfig, OssClient client, int delType)
        {

            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(ossConfig.OutputBucketName);
            if (delType == 1)
            {
                listObjectsRequest.Prefix = ossConfig.Folder + "/" + RunId + "/";
            }
            else if (delType == 2)
            {
                listObjectsRequest = new ListObjectsRequest(ossConfig.InputBucketName);
                listObjectsRequest.Prefix = ossConfig.Folder + "/" + FileName + FileType;
            }
            else if (delType == 3)
            {
                listObjectsRequest.Prefix = ossConfig.ImgFolder + "/" + RunId + ".jpg";
            }
            var result = client.ListObjects(listObjectsRequest);
            var keys = new List<string>();
            if (result.ObjectSummaries.Count() > 0) //该目录下有没有文件
            {
                foreach (var summary in result.ObjectSummaries)
                {
                    keys.Add(summary.Key);

                }
                DeleteObjectsRequest request;
                if (delType == 2)
                {
                    request = new DeleteObjectsRequest(ossConfig.InputBucketName, keys, false);
                }
                else
                {
                    request = new DeleteObjectsRequest(ossConfig.OutputBucketName, keys, false);
                }
                client.DeleteObjects(request);
            }
        }

        public int DelVod(string RunId, string FileName, string FileType)
        {
            NiurenOSSConfig ossConfig = new NiurenOSSConfig();
            var client = new OssClient(ossConfig.Endpoint, ossConfig.AccessKeyId, ossConfig.AccessKeySecret);
            try
            {
                for (int i = 1; i <= 3; i++) //此方法执行三次
                {
                    DelOssFile(RunId, FileName, FileType, ossConfig, client, i);
                }

                return vodUploadDAL.DelVod(RunId); //删除了文件最后删除自己的数据库记录

            }
            catch (Exception)
            {
                return -2;
            }

        }
代码写的不好,献丑了。主要就是这三个函数了,改改获取值的地方,再改改删除己方数据库的逻辑,就能用了。

删除就是找到你存放的路径,这些路径请自行配置好。

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页