热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#使用七牛云存储上传下载文件

项目需要将音视频文件上传服务器,考虑并发要求高,通过七牛来实现。直接上代码usingQiniu.IO;usingQiniu.IO.Resumable;usingQiniu.RPC;

项目需要将音视频文件上传服务器,考虑并发要求高,通过七牛来实现。

直接上代码

using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RPC;
using Qiniu.RS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace qiniuTest
{
    /// 
    /// 文件上传有两种方式:
    /// 一种是以普通方式直传文件,简称普通上传;
    /// 另一种方式是断点续上传,断点续上传在网络条件很一般的情况下也能有出色的上传速度,而且对大文件的传输非常友好。
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            Qiniu.Conf.Config.ACCESS_KEY = "6QQ7Cnz4bljdkQOWQ5UOAheVCAd0bCa7Tc5XXXXX";
            Qiniu.Conf.Config.SECRET_KEY = "9rUGnbFtvm-PLWcZeOR6ed9MUjZ4bKitf7YXXXXX";

            string bucket = "cvtehrmxz";
            string fileKey = "应用系统全貌图.png";
            GetFileStat(bucket, fileKey);

            //小文件直传
            string fileName = "业务功能架构图-IM和企业微信.jpg";
            PutFile(bucket, Guid.NewGuid().ToString() + fileName, "d:\\" + fileName);

            //在asp.net mvc中的文件上传
            //ResumablePutFile(bucket, Guid.NewGuid().ToString(), Path.Combine(path, Request.Form[0]));

            //大文件上传
            //string bigFileName = "eclipse-java-luna-SR1-win32-x86_64.zip";
            //ResumablePutFile(bucket, Guid.NewGuid().ToString() + bigFileName, "d:\\Software\\" + bigFileName);

            GetFile("7xXXXX.com1.z0.glb.clouddn.com", fileKey);
        }

        /// 
        /// 查看单个文件属性信息
        /// 
        /// 七牛云存储空间名
        /// 文件key,也就是文件名
        public static void GetFileStat(string bucket, string key)
        {
            RSClient client = new RSClient();
            Entry entry = client.Stat(new EntryPath(bucket, key));
            if (entry.OK)
            {
                Console.WriteLine("Hash: " + entry.Hash);
                Console.WriteLine("Fsize: " + entry.Fsize);
                Console.WriteLine("PutTime: " + entry.PutTime);
                Console.WriteLine("MimeType: " + entry.MimeType);
                Console.WriteLine("Customer: " + entry.Customer);
            }
            else
            {
                Console.WriteLine("Failed to Stat");
            }
        }


        /// 
        /// 删除单个文件
        /// 
        /// 文件所在的空间名
        /// 文件key
        public static void Delete(string bucket, string key)
        {
            Console.WriteLine("\n===> Delete {0}:{1}", bucket, key);
            RSClient client = new RSClient();
            CallRet ret = client.Delete(new EntryPath(bucket, key));
            if (ret.OK)
            {
                Console.WriteLine("Delete OK");
            }
            else
            {
                Console.WriteLine("Failed to delete");
            }
        }

        /// 
        /// 批量删除文件
        /// 
        /// 文件所在的空间名
        /// 文件key
        public static void BatchDelete(string bucket, string[] keys)
        {
            RSClient client = new RSClient();
            List EntryPaths = new List();
            foreach (string key in keys)
            {
                Console.WriteLine("\n===> Stat {0}:{1}", bucket, key);
                EntryPaths.Add(new EntryPath(bucket, key));
            }
            client.BatchDelete(EntryPaths.ToArray());
        }


        /// 
        /// 普通方式直传文件
        /// 
        /// 文件所在的空间名
        /// 您可以自行定义文件Key,一般GUID
        /// 文件路径+文件名
        public static void PutFile(string bucket, string key, string fname)
        {
            var policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra();
            IOClient client = new IOClient();
            client.PutFile(upToken, key, fname, extra);
        }

        /// 
        /// 断点续上传方式,传大文件用这种方式
        /// 
        /// 文件所在的空间名
        /// 您可以自行定义文件Key,一般GUID
        /// 文件路径+文件名
        public static void ResumablePutFile(string bucket, string key, string fname)
        {
            Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname);
            PutPolicy policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            Settings setting = new Settings();
            ResumablePutExtra extra = new ResumablePutExtra();
            ResumablePut client = new ResumablePut(setting, extra);
            client.PutFile(upToken, fname, Guid.NewGuid().ToString());
        }

        /// 
        /// Get方式获取文件
        /// 
        /// 文件域
        /// 文件Key
        public static void GetFile(string domain, string key)
        {
            System.Diagnostics.Process.Start("http://" + domain + "/" + key);
        }
    }
}

C#使用七牛云存储上传下载文件


推荐阅读
  • 4554:[Tjoi2016&Heoi2016]游戏 ... [详细]
  • Flutter 布局(四) Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth详解
    本文主要介绍Flutter布局中的Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth四种控件,详细介绍了其布局 ... [详细]
  • 动手动脑,无法自拔(3)课时作业6
    1.动手动脑(五子棋棋盘排布)(1)源程序(2)实验截图2.动手动脑(数字转换成汉字)(1)源程序(2)实验截图3.动手动脑(大数计算)(1)源程序 ... [详细]
  • P1025数的划分学傻了,学傻了,什么d ... [详细]
  • php课程Json格式规范需要注意的小细节
    JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgramming ... [详细]
  • gitlab重置password
    ruby没怎么学,自己搭建的gitlab的rootpassword又忘了。幸好看见此帖子,试验okhttp:roland.kierkels.netgitreset-your-git ... [详细]
  • 第8章 使用外部和内部链接
    8.1使用web地址LearnAboutafricanelephants. ... [详细]
  • 今天我们开始学习下拉及多级弹出菜单,包含以下内容和知识点:带下拉子菜单的导航菜单绝对定位和浮动的区别和运用css自适应宽度滑动门菜单一、带下拉子菜单的导航菜单下拉菜单在一些 ... [详细]
  • 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用java.util包中的Date类。这个类最主要的作用就是获取当前时间,我们来看下Date类的使用:使用Date类的 ... [详细]
  • java的流分两大类,字节流和字符流。一般在cc++中,一个字节是8位,java也是一样。但是,在cc++中一个字符,即char一般也是8位(可能机器不同会有所不同),但java为 ... [详细]
  • 1.Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时 ... [详细]
  • nginx+多个tomcat
    学习nginx的时候遇到的问题:nginx怎么部署两台tomcat?upstream在网上找的资源,我在nginx配置文件(nginx.conf)中添加了两个server。结果只显 ... [详细]
  • 前言:拿到一个案例,去分析:它该是做分类还是做回归,哪部分该做分类,哪部分该做回归,哪部分该做优化,它们的目标值分别是什么。再挑影响因素,哪些和分类有关的影响因素,哪些和回归有关的 ... [详细]
  • Acircleoffriendsisanetworkoffriendrelationships.IfAisafriendofB,thenBisconsideredafriendof ... [详细]
  • 由于同源策略的限制,满足同源的脚本才可以获取资源。虽然这样有助于保障网络安全,但另一方面也限制了资源的使用。那么如何实现跨域呢,以下是实现跨域的一些方法。 ... [详细]
author-avatar
cc晨晨V_842
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有