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);
}
}
}