using Aliyun.OpenServices.OpenStorageService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace CRM.CIS.Utils
{
public static class OSS
{
const string accessId = "";
const string accessKey = "";
const string endpoint = "http://oss-cn-beijing.aliyuncs.com/";
//从指定的bucket下载名为key的 数据
public static bool Download(string bucketName, string key, ref byte[] buffer)
{
bool result = false;
try
{
OssClient ossClient = new OssClient(new Uri(endpoint), accessId, accessKey);
// verify etag
var o = ossClient.GetObject(bucketName, key);//下载
using (var requestStream = o.Content)
{
buffer = ReadBuffer(requestStream, (int)o.Metadata.ContentLength);//从OSS读取
string eTag = ComputeContentMd5(buffer);//计算MD5校验
result = o.Metadata.ETag == eTag;//返回是否一致。
}
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
return result;
}
//读取缓存的函数
static byte[] ReadBuffer(Stream stream, int length)
{
byte[] buffer = new byte[length];
int index = 0;
do
{
int len = stream.Read(buffer, index, length - index);
index += len;
} while (index < length);
return buffer;
}
//计算校验码
public static string ComputeContentMd5(byte[] buffer)
{
using (var md5 = MD5.Create())
{
var data = md5.ComputeHash(buffer);
var sBuilder = new StringBuilder();
foreach (var t in data)
{
sBuilder.Append(t.ToString("x2"));
}
return sBuilder.ToString().ToUpper();
}
}
internal static string ComputeContentMd5(Stream inputStream)
{
using (var md5 = MD5.Create())
{
var data = md5.ComputeHash(inputStream);
var sBuilder = new StringBuilder();
foreach (var t in data)
{
sBuilder.Append(t.ToString("x2"));
}
return sBuilder.ToString().ToUpper();
}
}
//上传指定的名称key的buffer到bucketname,
public static bool Upload(string bucketName, string key, byte[] buffer)
{
bool result = false;
string etag = ComputeContentMd5(buffer);
OssClient client = new OssClient(endpoint, accessId, accessKey);
try
{
// 1. put object to specified output stream
using (var fs = new System.IO.MemoryStream(buffer))
{
var metadata = new ObjectMetadata();
metadata.UserMetadata.Add("mykey1", "myval1");
metadata.UserMetadata.Add("mykey2", "myval2");
metadata.ContentDisposition = "attachment; filename =" + key;
metadata.CacheControl = "No-Cache";
metadata.ContentType = "application/x-zip-compressed";
client.PutObject(bucketName, key, fs, metadata);
metadata = client.GetObjectMetadata(bucketName, key);
Console.WriteLine(string.Format("ETag={0} etag ={1}", metadata.ETag, etag));
result = metadata.ETag == etag;//检查校验
}
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
return result;
}
/// <summary>
/// 从xml中反序列化一个对象
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static T Read<T>(byte[] xml)
{
T ke = default(T);
XmlSerializer xs = new XmlSerializer(typeof(T));
if (xml != null)
{
XmlReader xr = XmlReader.Create(new MemoryStream(xml));
if (xs.CanDeserialize(xr))
{
ke = (T)xs.Deserialize(xr);
}
}
return ke;
}
/// <summary>
/// 将一个对象序列化为二进制xml
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static byte[] Write<T>(T t)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, t);
return ms.ToArray();
}
//压缩指定的buffer返回buffer[]
public static byte[] Compress(byte[] buffer)
{
byte[] cbytes = null;
//压缩
using (MemoryStream cms = new MemoryStream())
{
using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Compress))
{
//将数据写入基础流,同时会被压缩
gzip.Write(buffer, 0, buffer.Length);
}
cbytes = cms.ToArray();
}
return cbytes;
}
//解压被压缩的buffer返回byte[]
public static byte[] Decompress(byte[] buffer)
{
byte[] result;
using (MemoryStream dms = new MemoryStream())
{
using (MemoryStream cms = new MemoryStream(buffer))
{
using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Decompress))
{
byte[] bytes = new byte[1024];
int len = 0;
while ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
{
dms.Write(bytes, 0, len);
}
}
}
result = dms.ToArray();
}
return result;
}
//备份一个类名为CISData 的可序列化对象压缩并以filename命名上传至bucketname
public static bool Backup(string bucketname, string filename)
{
CISData dat = CISData.GetData();//得到此对象, 这个是你自己要序列化的类 。
byte[] buffer = OSS.Write<CISData>(dat);//序列化
byte[] buff2 = Compress(buffer);//压缩
bool ok = Upload(bucketname, filename + DateTime.Now.ToString("yyyyMMddHHmmss"), buff2);//上传, 两份 , 以日期结尾
if (ok) ok = Upload(bucketname, filename, buff2);//上传第二份, 以名字为主, 这个事我自己特殊使用 , 仅供参考
return ok;
}
//从OSS的bucktename恢复名为filename的文件
public static bool Restore(string bucketname, string filename)
{
byte[] buffer = new byte[] { };
bool ok = false;
if (Download(bucketname, filename, ref buffer))//首先下载
{
byte[] buff2 = Decompress(buffer);//解压缩
CISData dat = OSS.Read<CISData>(buff2);//反序列化至对象
ok = CISData.SetData(dat);如果反序列化数据正常返回成功, 这里根据你自己的需要
}
return ok;
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。