using COSXML.Common; using System.IO; using COSXML.Model.Tag; using COSXML.Utils; using COSXML.CosException; using COSXML.Network; namespace COSXML.Model.Object { /// /// 简单上传对象 /// /// public sealed class PutObjectRequest : ObjectRequest { private static string TAG = typeof(PutObjectRequest).FullName; /// /// 本地文件路径 /// private string srcPath; /// /// 上传文件指定起始位置 /// private long fileOffset = 0L; /// /// 上传data数据 /// private byte[] data; /// /// 上传指定内容的长度 /// private long contentLength = -1L; /// /// 上传回调 /// private COSXML.Callback.OnProgressCallback progressCallback; /// /// 上传整个文件 /// /// /// /// public PutObjectRequest(string bucket, string key, string srcPath) :this(bucket, key, srcPath, -1L, -1L) { } /// /// 上传文件的指定内容 /// /// /// /// /// 文件指定起始位置 /// 文件指定内容长度 public PutObjectRequest(string bucket, string key, string srcPath, long fileOffset, long needSendLength) :base(bucket, key) { this.method = CosRequestMethod.PUT; this.srcPath = srcPath; this.fileOffset = fileOffset < 0 ? 0 : fileOffset; this.contentLength = needSendLength < 0 ? -1L : needSendLength; } /// /// 上传data数据 /// /// /// /// public PutObjectRequest(string bucket, string key, byte[] data) : base(bucket, key) { this.method = CosRequestMethod.PUT; this.data = data; } /// /// 上传回调 /// /// public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback) { this.progressCallback = progressCallback; } public override void CheckParameters() { if (srcPath == null && data == null) throw new CosClientException((int)(CosClientError.INVALID_ARGUMENT), "data source = null"); if (srcPath != null) { if (!File.Exists(srcPath)) throw new CosClientException((int)(CosClientError.INVALID_ARGUMENT), "file not exist"); } base.CheckParameters(); } public override Network.RequestBody GetRequestBody() { RequestBody body = null; if (srcPath != null) { FileInfo fileInfo = new FileInfo(srcPath); if (contentLength == -1 || contentLength + fileOffset > fileInfo.Length) { contentLength = fileInfo.Length - fileOffset; } body = new FileRequestBody(srcPath, fileOffset, contentLength); body.ProgressCallback = progressCallback; } else if (data != null) { body = new ByteRequestBody(data); body.ProgressCallback = progressCallback; } return body; } /// /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private /// /// /// public void SetCosACL(string cosACL) { if (cosACL != null) { SetRequestHeader(CosRequestHeaderKey.X_COS_ACL, cosACL); } } /// /// 最大上传速度,单位是 bit/s /// /// public void LimitTraffic(long rate) { SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString()); } /// /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private /// /// /// public void SetCosACL(CosACL cosACL) { SetRequestHeader(CosRequestHeaderKey.X_COS_ACL, EnumUtils.GetValue(cosACL)); } /// /// 赋予被授权者读的权限 /// /// /// public void SetXCosGrantRead(GrantAccount grantAccount) { if (grantAccount != null) { SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_READ, grantAccount.GetGrantAccounts()); } } /// /// 赋予被授权者写的权限 /// /// /// public void SetXCosGrantWrite(GrantAccount grantAccount) { if (grantAccount != null) { SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_WRITE, grantAccount.GetGrantAccounts()); } } /// /// 赋予被授权者所有的权限 /// /// /// public void SetXCosReadWrite(GrantAccount grantAccount) { if (grantAccount != null) { SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_FULL_CONTROL, grantAccount.GetGrantAccounts()); } } } }