码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-01-13 12:43:03      阅读:1212      评论:0      收藏:0      [点我收藏+]

标签:

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

直接上代码

 

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
{
    /// <summary>
    /// 文件上传有两种方式:
    /// 一种是以普通方式直传文件,简称普通上传;
    /// 另一种方式是断点续上传,断点续上传在网络条件很一般的情况下也能有出色的上传速度,而且对大文件的传输非常友好。
    /// </summary>
    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);
        }

        /// <summary>
        /// 查看单个文件属性信息
        /// </summary>
        /// <param name="bucket">七牛云存储空间名</param>
        /// <param name="key">文件key,也就是文件名</param>
        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");
            }
        }


        /// <summary>
        /// 删除单个文件
        /// </summary>
        /// <param name="bucket">文件所在的空间名</param>
        /// <param name="key">文件key</param>
        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");
            }
        }

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


        /// <summary>
        /// 普通方式直传文件
        /// </summary>
        /// <param name="bucket">文件所在的空间名</param>
        /// <param name="key">您可以自行定义文件Key,一般GUID</param>
        /// <param name="fname">文件路径+文件名</param>
        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);
        }

        /// <summary>
        /// 断点续上传方式,传大文件用这种方式
        /// </summary>
        /// <param name="bucket">文件所在的空间名</param>
        /// <param name="key">您可以自行定义文件Key,一般GUID</param>
        /// <param name="fname">文件路径+文件名</param>
        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());
        }

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

 

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

标签:

原文地址:http://www.cnblogs.com/starcrm/p/5126702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!