static void Main(string[] args) { Auth auth = EnvUtility.EnvAuth(); Config config = EnvUtility.EnvConfig(); // 上传到这个 bucket string bucket = "umu618-docs"; string key = "U-1K.txt"; // 上传前先删除 BucketManager bm = new BucketManager(auth, config); HttpResult result = bm.Delete(bucket, key); Console.WriteLine("---Delete---\n{0}", result.ToString()); // 在内存构造一个文件内容:1024 个 U const long dataSize = 1024; byte[] data = new byte[dataSize]; for (int i = 0; i < dataSize; ++i) { data[i] = 85; } // 一个小时的超时,转为 UnixTime 毫秒数 long deadline = UnixTimestamp.GetUnixTimestamp(3600) * 1000; string putPolicy = "{\"scope\": \"" + bucket + "\",\"deadline\": \"" + deadline + "\"}"; Console.WriteLine("----putPolicy----\n{0}", putPolicy); SimpleUpload su = new SimpleUpload(auth, config); result = su.UploadData(data, putPolicy, key); Console.WriteLine("---UploadData---\n{0}", result.ToString()); // 下面输出的 HASH 值应该一样 if ((int)HttpStatusCode.OK == result.Code) { Console.WriteLine("UploadResult: {0}", Base64.UrlSafeBase64Decode(result.Text)); } Console.WriteLine("UploadData's ETag: {0}", ETag.ComputeEtag(data)); }
public static HttpResult UploadStream(Stream stream, SliceUpload uploader, string policy, string file) { Auth auth = EnvUtility.EnvAuth(); Config config = EnvUtility.EnvConfig(); // 上传到这个 bucket string bucket = "csj-zyk"; string key = file; // 上传前先删除 BucketManager bm = new BucketManager(auth, config); HttpResult result = bm.Delete(bucket, key); long dataSize = GetDataSize(stream.Length); byte[] data = new byte[dataSize]; stream.Read(data, 0, data.Length); // 最后合成文件时的 hash Console.WriteLine("ETag of uploading data: {0}", ETag.ComputeEtag(data)); // 一个小时的超时,转为 UnixTime 毫秒数 long deadline = UnixTimestamp.GetUnixTimestamp(3600) * 1000; string putPolicy = "{\"scope\": \"" + bucket + ":" + key + "\",\"deadline\": \"" + deadline + "\"}"; string uploadToken = auth.CreateUploadToken(putPolicy); // 第一个分片不宜太大,因为可能遇到错误,上传太大是白费流量和时间! const long blockSize = 4 * 1024 * 1024; const int firstChunkSize = 1024; SliceUpload su = uploader; // new SliceUpload(auth, config); result = su.MakeBlock(blockSize, 0, data, 0, firstChunkSize, uploadToken); if ((int)HttpStatusCode.OK == result.Code) { long blockCount = (dataSize + blockSize - 1) / blockSize; string[] contexts = new string[blockCount]; JObject jo = JObject.Parse(result.Text); contexts[0] = jo["ctx"].ToString(); // 上传第 1 个 block 剩下的数据 result = su.Bput(contexts[0], firstChunkSize, data, firstChunkSize, (int)(blockSize - firstChunkSize), uploadToken); if ((int)HttpStatusCode.OK == result.Code) { jo = JObject.Parse(result.Text); contexts[0] = jo["ctx"].ToString(); // 上传后续 block,每次都是一整块上传 for (int blockIndex = 1; blockIndex < blockCount; ++blockIndex) { long leftSize = dataSize - blockSize * blockIndex; int chunkSize = (int)(leftSize > blockSize ? blockSize : leftSize); result = su.MakeBlock(chunkSize, blockIndex, data, (int)(blockSize * blockIndex), chunkSize, uploadToken); if ((int)HttpStatusCode.OK == result.Code) { jo = JObject.Parse(result.Text); contexts[blockIndex] = jo["ctx"].ToString(); } else { return(result); } } // 合成文件,注意与前面打印的 ETag 对比 result = su.MakeFile(dataSize, key, contexts, uploadToken); } } return(result); }
static void Main(string[] args) { Auth auth = EnvUtility.EnvAuth(); Config config = EnvUtility.EnvConfig(); // 上传到这个 bucket string bucket = "umu618-docs"; string key = "U-6M.txt"; // 上传前先删除 BucketManager bm = new BucketManager(auth, config); HttpResult result = bm.Delete(bucket, key); Console.WriteLine("---Delete---\n{0}", result.ToString()); // 在内存构造一个文件内容:2M 个 U,2M 个 M, 2M 个 U const long dataSize = 6 * 1024 * 1024; byte[] data = new byte[dataSize]; int i = 0; for (; i < dataSize / 3; ++i) { data[i] = 85; } for (; i < dataSize / 3 * 2; ++i) { data[i] = 77; } for (; i < dataSize; ++i) { data[i] = 85; } // 最后合成文件时的 hash Console.WriteLine("ETag of uploading data: {0}", ETag.ComputeEtag(data)); // 一个小时的超时,转为 UnixTime 毫秒数 long deadline = UnixTimestamp.GetUnixTimestamp(3600) * 1000; string putPolicy = "{\"scope\": \"" + bucket + ":" + key + "\",\"deadline\": \"" + deadline + "\"}"; Console.WriteLine("----putPolicy----\n{0}", putPolicy); string uploadToken = auth.CreateUploadToken(putPolicy); Console.WriteLine("----uploadToken----\n{0}", uploadToken); // 第一个分片不宜太大,因为可能遇到错误,上传太大是白费流量和时间! const long blockSize = 4 * 1024 * 1024; const int firstChunkSize = 1024; SliceUpload su = new SliceUpload(auth, config); result = su.MakeBlock(blockSize, 0, data, 0, firstChunkSize, uploadToken); Console.WriteLine("---MakeBlock---\n{0}", result.ToString()); if ((int)HttpStatusCode.OK == result.Code) { long blockCount = (dataSize + blockSize - 1) / blockSize; string[] contexts = new string[blockCount]; JObject jo = JObject.Parse(result.Text); contexts[0] = jo["ctx"].ToString(); // 上传第 1 个 block 剩下的数据 result = su.Bput(contexts[0], firstChunkSize, data, firstChunkSize, (int)(blockSize - firstChunkSize), uploadToken); Console.WriteLine("---Bput---\n{0}", result.ToString()); if ((int)HttpStatusCode.OK == result.Code) { jo = JObject.Parse(result.Text); contexts[0] = jo["ctx"].ToString(); // 上传后续 block,每次都是一整块上传 for (int blockIndex = 1; blockIndex < blockCount; ++blockIndex) { long leftSize = dataSize - blockSize * blockIndex; int chunkSize = (int)(leftSize > blockSize ? blockSize : leftSize); result = su.MakeBlock(chunkSize, blockIndex, data, (int)(blockSize * blockIndex), chunkSize, uploadToken); Console.WriteLine("---MakeBlock---\n{0}", result.ToString()); if ((int)HttpStatusCode.OK == result.Code) { jo = JObject.Parse(result.Text); contexts[blockIndex] = jo["ctx"].ToString(); } else { Console.WriteLine("----Exit with error----"); return; } } // 合成文件,注意与前面打印的 ETag 对比 result = su.MakeFile(dataSize, key, contexts, uploadToken); Console.WriteLine("---MakeFile---\n{0}", result.ToString()); } } }
static void Main(string[] args) { Auth auth = EnvUtility.EnvAuth(); Config config = EnvUtility.EnvConfig(); BucketManager bm = new BucketManager(auth, config); // 枚举所有 Buckets HttpResult result = bm.Buckets(); Console.WriteLine("---Buckets---\n{0}", result.ToString()); if ((int)HttpStatusCode.OK == result.Code) { JObject jo = JObject.Parse(result.Text); var buckets = jo["buckets"]; Console.WriteLine("{0} Buckets found:", buckets.Count()); int i = 0; foreach (var bucket in buckets) { // 打印 Bucket 名字 Console.WriteLine("{0}\t{1}", ++i, bucket["name"].ToString()); } } // BucketManager 还有很多函数,以下示范比较粗糙,您需要自己改改! result = bm.BucketList("umu618-docs", 0, null, 1); Console.WriteLine("---BucketList---\n{0}", result.ToString()); result = bm.BucketStat("chentemp|umu618-docs", "2017-07-01", "2017-09-01"); Console.WriteLine("---BucketStat---\n{0}", result.ToString()); result = bm.Stat("umu618-docs", "Go.pdf"); Console.WriteLine("---Stat---\n{0}", result.ToString()); string[] keys = { "Go.pdf" }; result = bm.Prefetch("umu618-docs", keys); Console.WriteLine("---Prefetch---\n{0}", result.ToString()); result = bm.ImageInfo("http://images.w.wcsapi.biz.matocloud.com/1.png"); Console.WriteLine("---ImageInfo---\n{0}", result.ToString()); result = bm.Exif("http://chentemp.w.wcsapi.biz.matocloud.com/IMG_1140.JPG"); Console.WriteLine("---Exif---\n{0}", result.ToString()); result = bm.AvInfo("http://images.w.wcsapi.biz.matocloud.com/1.mp4"); Console.WriteLine("---AvInfo---\n{0}", result.ToString()); result = bm.AvInfo2("http://images.w.wcsapi.biz.matocloud.com/1.mp4"); Console.WriteLine("---AvInfo2---\n{0}", result.ToString()); result = bm.Delete("umu618-docs", "/temp/"); Console.WriteLine("---Delete---\n{0}", result.ToString()); result = bm.Move("umu618-docs", "Go.pdf", "umu618-docs", "golang/Go.pdf"); Console.WriteLine("---Move---\n{0}", result.ToString()); result = bm.Copy("umu618-docs", "data.txt", "umu618-docs", "data.txt"); Console.WriteLine("---Copy---\n{0}", result.ToString()); result = bm.Decompression("umu618-docs", "各种录音程序.7z", "7z", "umu618-docs:exe", "umu618-docs:exe/UMU.list"); Console.WriteLine("---Decompression---\n{0}", result.ToString()); Dictionary <string, string> d = new Dictionary <string, string>(); d.Add("umu618-docs:/", "umu618-docs:UMU.list"); d.Add("umu618-docs:temp", "umu618-docs:temp/UMU.list"); result = bm.Decompression("umu618-docs", "各种录音程序.7z", "7z", d); Console.WriteLine("---Decompression---\n{0}", result.ToString()); result = bm.PersistentStatus("e534f75d41534f468a7efe9a51dc90ad"); Console.WriteLine("---PersistentStatus---\n{0}", result.ToString()); result = bm.SetDeadline("umu618-docs", "Go.pdf", -1); Console.WriteLine("---SetDeadline---\n{0}", result.ToString()); }