using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Windows.Forms;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace _1200gFtp
{
///
/// 阿里云上传下载数据类
///
public class OssUnity
{
public readonly OssClient _client;
const string endpoint = "http://oss-cn-beijing.aliyuncs.com";
//北京科麦迪账号下的 华东2 上海 oss
const string accessId = "LTAI5tA3YFzqR2qHSLYZP68T";
const string accessKey = "efjTmIWXCGcJO4vmnHnNhKsuXbiVo5";
public OssUnity()
{
_client = new OssClient(endpoint, accessId, accessKey);
}
///
/// Bucket数据容器,出错返回错误信息.
///
/// Bucket名称
/// 错误信息
/// 是否执行成功
public bool CreateBucket(string bucketName, out string msg)
{
bool reslut = true;
msg = string.Empty;
try
{
var buckets = _client.ListBuckets();
if (buckets.All(b => b.Name != bucketName))
{
_client.CreateBucket(bucketName);
}
}
catch (OssException ex)
{
reslut = false;
msg = ex.Message;
}
return reslut;
}
///
/// 上传文件到Bucket容器
///
/// Bucket容器
/// Key值
/// 文件路径
/// 返回MD5值
public string PutObject(String bucketName, String key, String path)
{
using (var fs = File.Open(path, FileMode.Open))
{
var metadata = new ObjectMetadata { ContentLength = fs.Length };
var result = _client.PutObject(bucketName, key, fs, metadata);
return result.ETag;
}
}
///
/// 下载数据
///
/// 进度条
/// 提示文本框
/// 文件保存路径
/// bucket名称
/// KEY值
///
public bool DownloadData(ProgressBar pb, Label lbl, string saveFileName, string bucketName, string key)
{
bool result = false;
short ticks = 0;
try
{
if (pb != null)
{
//if (pb.InvokeRequired)
//{
// pb.BeginInvoke((Action)(() =>
// {
// pb.Maximum = 100;
// }));
//}
//else
pb.Maximum = 100;
}
var obj = _client.GetObject(bucketName, key);
using (var requestStream = obj.Content)
{
var buf = new byte[1024];
using (var fs = File.Open(saveFileName, FileMode.OpenOrCreate))
{
var len = 0;
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
float percent = fs.Length / (float)obj.Metadata.ContentLength;
var n = (int)Math.Ceiling(percent * 100);
Application.DoEvents();
if (pb != null)
{
pb.Value = n <= 100 ? n : 100;
//pb.Refresh();
}
if (lbl != null)
{
lbl.Text = percent.ToString("00.00%");
//lbl.Refresh();
}
}
}
}
result = true;
}
catch (Exception ex)
{
ticks++;
if (ticks < 3)
{
DownloadData(pb, lbl, saveFileName, bucketName, key);
}
else
{
// Watching.Write(@"下载出错:", ex);
if (lbl != null)
lbl.Text = "下载出错,请稍后再试!";
}
}
return result;
}
///
/// 判断OSS上的文件是否存在
///
/// BUCKET NAME
/// KEY
///
public bool Exists(string bucketName, string key)
{
bool flag = true;
try
{
OssObject obj = _client.GetObject(bucketName, key);
obj.Dispose();
}
catch (Exception)
{
flag = false;
}
return flag;
}
///
/// 下载oss上的文件
///
///
///
///
public void GetObject(String bucketName, string key, string localFilePath)
{
var object1 = _client.GetObject(bucketName, key);
using (var requestStream = object1.Content)
{
var buf = new byte[1024];
var fs = File.Open(localFilePath, FileMode.OpenOrCreate);
int len;
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
}
fs.Close();
}
}
}
}