private static readonly OssClient _ossClient = new OssClient(_endpoint, _accessId, _accessKey);
public bool PutSingleObject(String bucketName, String objectName, String fileToUpload)
{
bool resul = false;
try
{
OssClient client = new OssClient(_endpoint, _accessId, _accessKey);
using (var fs = File.Open(fileToUpload, FileMode.Open))
{
var metadata = new ObjectMetadata();
metadata.UserMetadata.Add("mykey1", "myval1");
metadata.CacheControl = "No-Cache";
metadata.ContentLength = fs.Length;
client.PutObject(bucketName, objectName, fs, metadata);
MessageBox.Show(client.ToString());
}
resul=true;
}
catch (OssException )
{
MessageBox.Show("请检查网络");
resul = false;
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
// ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception )
{
MessageBox.Show("请检查网络");
resul = false;
// Console.WriteLine("Failed with error info: {0}", ex.Message);
}
return resul;
}
二 、多文件上传 (带进度条) UploadMultipart
public class UploadThread
{
public delegate void ShowProgressDelegate(int totalStep, int currentStep);
private ShowProgressDelegate process_;
private Control invokeControl_;
private long totalSize;
private long currentSize;
private long currentsizenum;
public long TotalSize
{
get { return totalSize; }
}
public long CurrentSize
{
get { return this.currentsizenum; }
}
public float CurrentProgress
{
get
{
if (this.totalSize != 0)
{
return (float)this.currentsizenum * 100 / (float)this.totalSize;
}
else
{
return 0;
}
}
}
public string UploadMultipart(String bucketName, String objectName, String fileToUpload, int partSize, ShowProgressDelegate prosess, Control t)
{
invokeControl_ = t;
process_ = prosess;
var uploadId = InitiateMultipartUpload(bucketName, objectName);
var partETags = UploadParts(bucketName, objectName, fileToUpload, uploadId, partSize);
var completeResult = CompleteUploadPart(bucketName, objectName, uploadId, partETags);
return completeResult.BucketName;
}
private static string InitiateMultipartUpload(String bucketName, String objectName)
{
var request = new InitiateMultipartUploadRequest(bucketName, objectName);
var result = _ossClient.InitiateMultipartUpload(request);
return result.UploadId;
}
private List<PartETag> UploadParts(String bucketName, String objectName, String fileToUpload, String uploadId, int partSize)
{
var fi = new FileInfo(fileToUpload);
var fileSize = fi.Length;
totalSize = fileSize;
var partCount = fileSize / partSize;
if (fileSize % partSize != 0)
{