ECGGather/1200gFtp/OssUnity.cs

189 lines
6.2 KiB
C#
Raw Normal View History

2024-12-25 17:21:40 +08:00
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
{
/// <summary>
/// 阿里云上传下载数据类
/// </summary>
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);
}
/// <summary>
/// Bucket数据容器,出错返回错误信息.
/// </summary>
/// <param name="bucketName">Bucket名称</param>
/// <param name="msg">错误信息</param>
/// <returns>是否执行成功</returns>
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;
}
/// <summary>
/// 上传文件到Bucket容器
/// </summary>
/// <param name="bucketName">Bucket容器</param>
/// <param name="key">Key值</param>
/// <param name="path">文件路径</param>
/// <returns>返回MD5值</returns>
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;
}
}
/// <summary>
/// 下载数据
/// </summary>
/// <param name="pb">进度条</param>
/// <param name="lbl">提示文本框</param>
/// <param name="saveFileName">文件保存路径</param>
/// <param name="bucketName">bucket名称</param>
/// <param name="key">KEY值</param>
/// <returns></returns>
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;
}
/// <summary>
/// 判断OSS上的文件是否存在
/// </summary>
/// <param name="bucketName">BUCKET NAME</param>
/// <param name="key">KEY</param>
/// <returns></returns>
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;
}
/// <summary>
/// 下载oss上的文件
/// </summary>
/// <param name="bucketName"></param>
/// <param name="key"></param>
/// <param name="localFilePath"></param>
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();
}
}
}
}