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 AppendObjectRequest : ObjectRequest
{
private static string TAG = typeof(PutObjectRequest).FullName;
///
/// 本地文件路径
///
private string srcPath;
///
/// 追加到对象的位置
///
private long appendPosition;
///
/// 读文件的起始位置
///
private long fileOffset = 0;
///
/// 上传data数据
///
private byte[] data;
///
/// 上传指定内容的长度
///
private long contentLength = -1L;
///
/// 上传回调
///
private COSXML.Callback.OnProgressCallback progressCallback;
///
/// 上传文件的指定范围到对象末尾
///
///
///
///
/// 追加到对象的位置,首次上传应为0
/// 读文件的起始位置
/// 文件指定内容长度
public AppendObjectRequest(string bucket, string key, string srcPath, long appendPosition, long fileOffset, long needSendLength)
: this(bucket, key, srcPath, appendPosition)
{
this.fileOffset = fileOffset < 0 ? 0 : fileOffset;
this.contentLength = needSendLength < 0 ? -1L : needSendLength;
}
///
/// 上传文件到对象末尾
///
///
///
///
/// 追加到对象的位置,首次上传为0
public AppendObjectRequest(string bucket, string key, string srcPath, long appendPosition)
: base(bucket, key)
{
this.method = CosRequestMethod.POST;
this.srcPath = srcPath;
this.appendPosition = appendPosition < 0 ? 0 : appendPosition;
this.queryParameters.Add("append", null);
this.queryParameters.Add("position", appendPosition.ToString());
}
///
/// 上传data数据到对象末尾
///
///
///
///
///
public AppendObjectRequest(string bucket, string key, byte[] data, long appendPosition)
: base(bucket, key)
{
this.appendPosition = appendPosition < 0 ? 0 : appendPosition;
this.queryParameters.Add("append", null);
this.queryParameters.Add("position", appendPosition.ToString());
this.method = CosRequestMethod.POST;
this.data = data;
}
///
/// 上传回调
///
///
public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
{
this.progressCallback = progressCallback;
}
///
/// 设置 Object 的存储级别
///
///
///
public void SetCosStorageClass(string cosStorageClass)
{
SetRequestHeader(CosRequestHeaderKey.X_COS_STORAGE_CLASS, cosStorageClass);
}
public override void CheckParameters()
{
if (srcPath == null && data == null)
{
throw new CosClientException((int)(CosClientError.InvalidArgument), "data source = null");
}
if (srcPath != null)
{
if (!File.Exists(srcPath))
{
throw new CosClientException((int)(CosClientError.InvalidArgument), "file not exist");
}
}
if(fileOffset > 0) {
}
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)
{
SetCosACL(EnumUtils.GetValue(cosACL));
}
///
/// 赋予被授权者读的权限
///
///
///
public void SetXCosGrantRead(GrantAccount grantAccount)
{
if (grantAccount != null)
{
SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_READ, grantAccount.GetGrantAccounts());
}
}
///
/// 赋予被授权者所有的权限
///
///
///
public void SetXCosReadWrite(GrantAccount grantAccount)
{
if (grantAccount != null)
{
SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_FULL_CONTROL, grantAccount.GetGrantAccounts());
}
}
}
}