开发者社区> 问答> 正文

使用CSharp对象序列化数据解压缩备份在OSS上(Restore,Backup)

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


展开
收起
麦壳饼 2015-04-13 21:19:20 14573 0
4 条回答
写回答
取消 提交回答
  • Re使用CSharp对象序列化数据解压缩备份在OSS上(Restore,Backup)
    顶一个。
    2015-04-22 11:05:07
    赞同 展开评论 打赏
  • Re使用CSharp对象序列化数据解压缩备份在OSS上(Restore,Backup)
    2015-04-18 14:00:07
    赞同 展开评论 打赏
  • LT是个伪程序员
    好代码必须顶一个!
    2015-04-16 16:47:44
    赞同 展开评论 打赏
  • Re使用C对象序列化数据解压缩备份在OSS上(Restore,Backup)
    能活的券么?这是最简单的应用, 可以吗?
    2015-04-13 21:19:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载