/// <summary> /// 开始上传 /// </summary> /// <returns></returns> public Task <string> Upload() { if (string.IsNullOrWhiteSpace(Bucket)) { throw new ArgumentNullException("Bucket"); } if (string.IsNullOrWhiteSpace(FullName)) { throw new ArgumentNullException("FullName"); } if (Content == null) { throw new ArgumentNullException("Content"); } if (Bucket.Equals("top-configs", StringComparison.OrdinalIgnoreCase)) { throw new NotSupportedException("不支持操作该节点"); } var host = string.Format("{0}.oss-cn-hangzhou{1}.aliyuncs.com", Bucket, OSS.AliyunECS ? "-internal" : ""); var www = new HttpRequest(host); www.Method = HttpRequestMethod.PUT; www.Path = FullName; www.Headers["Content-Type"] = ContentType ?? "application/octet-stream"; www.Headers["Content-Length"] = "-1"; www.Headers["Date"] = DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T", CultureInfo.InvariantCulture); www.Headers["Host"] = host; //www.Headers["Expect"] = "100-continue"; www.FormBody.Write(Content); OssSigner.Sign(www); return(www.GetString()); }
/// <summary> /// 删除文件 /// </summary> /// <param name="name"></param> /// <param name="bucket"></param> /// <returns></returns> public static Task DeleteFile(string name, string bucket) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); } if (string.IsNullOrWhiteSpace(bucket)) { throw new ArgumentNullException("bucket"); } if (bucket.Equals("top-configs", StringComparison.OrdinalIgnoreCase)) { throw new NotSupportedException("不支持操作该节点"); } const string xml = @"<?xml version=""1.0""?> <Delete> <Quiet>true</Quiet> <Object> <Key>{0}</Key> </Object> </Delete>"; var content = Encoding.UTF8.GetBytes(string.Format(xml, name)); var host = string.Format("{0}.oss-cn-hangzhou{1}.aliyuncs.com", bucket, OSS.AliyunECS ? "-internal" : ""); var www = new HttpRequest(host); www.Method = HttpRequestMethod.POST; www.Path = "?delete"; www.Headers["Date"] = DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T", System.Globalization.CultureInfo.InvariantCulture); www.Headers["Content-Type"] = ""; www.Headers["Content-Length"] = content.Length.ToString(); www.Headers["Content-MD5"] = MD5Content(content); www.FormBody.Write(content); OssSigner.Sign(www); Trace.WriteLine(bucket + "/" + name, "删除OSS文件"); return(www.GetString()); }