大部分云服务都是要money的,做一个无公司无团队的独行者,当然是一分钱都不愿意投入了。在查看了很长时间后,发现所有的云服务中,对象存储服务是价格最低的,而腾讯的此项服务,在一定的范围内还是可以免费使用的,只要是不超过一定的存储容量,不超过一定的流量。OK,这就足够了,发挥想象:一个可以编程的存贮空间,最简单也可以保存着软件的升级信息,用于客户端的软件升级吧。

           使用腾讯云的步骤大约如下:

           1、申请试用帐户:https://www.qcloud.com/act/try?t=cvm

           2、下载SDK,注意这个SDK如果在项目中引用,记得要把一些类及方法修改为public

           3、依据SDK返回API数据,这个数据是json格式的,当然要转换,方法是找一个在线的解析网站,将json数据直接转成.net可以使用的类:http://json2csharp.chahuo.com/

           4、到了这里,就可以用我们熟悉的编程方式进行数据处理了,比如同步本地数据到云端,同步云端数据到客户端。值得注意的是下载文件需要http方式,因为SDK中没有提供相应的API。

下面是同步数据到云端的代码,上传与此基本上相同。

 

IList<CompareFile> sourceFileList = new List<CompareFile>();
IList<CompareFile> RemoteFileList = new List<CompareFile>();
CosCloud cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY);

List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息
public MainWindow()
{
InitializeComponent();
this.textBox.Text = strDir;
this.textBoxRemote.Text = remoteUrl;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadSource();
LoadRemote();
//设置远端需要删除的文件
foreach (var v in RemoteFileList)
{
var existFile = (from p in sourceFileList
where p.FileName == v.FileName
select p).SingleOrDefault();
if (existFile == null)
{
v.IsFileExist = false;
}
else
{
v.IsFileExist = true;
}
}

//设置本地需要更新的文件
foreach (var v in sourceFileList)
{
var exit = (from p in RemoteFileList
where p.FileName == v.FileName
select p).SingleOrDefault();
if (exit == null)
{
v.IsFileExist = false;
}
else
{
if (v.SHA == exit.SHA)
{
v.IsShaSame = true;
}
else
{
v.IsShaSame = false;
}
v.IsFileExist = true;
}
}
this.dataGrid.ItemsSource = null;
this.dataGrid.ItemsSource = sourceFileList;
this.dataGrid1.ItemsSource = null;
this.dataGrid1.ItemsSource = RemoteFileList;
}

private void button_Click(object sender, RoutedEventArgs e)
{

}
private void button1_Click(object sender, RoutedEventArgs e)
{
string result = null;

foreach (var file in RemoteFileList.Where(p=>p.IsFileExist == false))
{
result = cos.DeleteFile(bucketName, file.FullName);
MessageBox.Show(result);
}

//同步本地文件到腾读云端
foreach (var file in sourceFileList.Where(p=>!p.IsFileExist || !p.IsShaSame))
{
if (!file.IsFileExist)
{
result = cos.UploadFile(bucketName, file.FileName, file.FullName);

}
else
{
result = cos.DeleteFile(bucketName, file.FileName);
result = cos.UploadFile(bucketName, file.FileName, file.FullName);
}
}
}


private void LoadSource()
{
Assembly myAssembly = Assembly.GetEntryAssembly();
string path = myAssembly.Location;
DirectoryInfo dr = new DirectoryInfo(path);

//path = dr.Parent; //当前目录的上一级目录

//IList<FileInfo> listFiles = GetAllFilesInDirectory(strDir);
GetAllFilesInDirectory(strDir);
//this.dataGrid.ItemsSource = listFiles;

foreach (var v in listFiles)
{
CompareFile file = new CompareFile();
file.FileName = "/" + v.FullName.Replace(strDir, "").Replace(@"\",@"/");
file.FullName = v.FullName;
file.SHA = QCloud.CosApi.Common.SHA1.GetSHA1(v.FullName);
sourceFileList.Add(file);

}

}


public void GetAllFilesInDirectory(string strDirectory)
{
DirectoryInfo directory = new DirectoryInfo(strDirectory);
DirectoryInfo[] directoryArray = directory.GetDirectories();
FileInfo[] fileInfoArray = directory.GetFiles();
if (fileInfoArray.Length > 0)
{
listFiles.AddRange(fileInfoArray);
}
//遍历目录
foreach (DirectoryInfo _directoryInfo in directoryArray)
{
DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName);
DirectoryInfo[] directoryArrayA = directoryA.GetDirectories();
FileInfo[] fileInfoArrayA = directoryA.GetFiles();
if (fileInfoArrayA.Length > 0)
{
GetAllFilesInDirectory(_directoryInfo.FullName);
}
}
}

private void LoadRemote()
{
GetAllRemoteFileInDirectory("/");

}

private void GetAllRemoteFileInDirectory(string path)

{

try
{
var result = "";


result = cos.GetFolderList(bucketName, path, 100, "", 0, QCloud.CosApi.Api.FolderPattern.Both);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var p1 = serializer.Deserialize<RootObject>(result);
//MessageBox.Show(p1.message);
foreach (var v in p1.data.infos.Where(p=>p.sha != null))
{
//CompareFile file = new CompareFile();
//file.FileName = v.name;
//file.SHA = v.sha;

//file.FullName = v.access_url.Replace(remoteUrl, "");
//file.FullName = HttpUtility.UrlDecode(file.FullName, Encoding.GetEncoding("UTF-8"));
////file.FullName = v.source_url;
//RemoteFileList.Add(file);
////fileList.Add(file);
CompareFile file = new CompareFile();
file.FileName = v.access_url.Replace(remoteUrl, "");
file.FileName = HttpUtility.UrlDecode(file.FileName, Encoding.GetEncoding("UTF-8"));
file.SHA = v.sha;
file.FullName = v.source_url;
RemoteFileList.Add(file);
}
foreach (var v in p1.data.infos.Where(p => p.sha == null))
{
string newPath = path + v.name + "/";
GetAllRemoteFileInDirectory(newPath);
}


}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}

           

posted on 2016-07-20 08:29  风可以追我  阅读(899)  评论(0编辑  收藏  举报