分片传输规则

1.不能超过10000片,
2.每片必须大于100KB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyun.OSS; namespace OssDemo
{
public partial class Form1 : Form
{
private string _endpoint = "oss-cn-beijing.aliyuncs.com";
// accessKey请登录https://ak-console.aliyun.com/#/查看
private string _accessKeyId = "";
private string _accessKeySecret = "";
private int partSize = **;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
lblStatus.Text = "Ready";
} private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var fileToUpload = openFileDialog1.FileName;
var filename = Path.GetFileName(fileToUpload);
var client = new OssClient(_endpoint, _accessKeyId, _accessKeySecret);
try
{
string bucketName = "myoss";
string key = filename; // 开始Multipart Upload
var request = new InitiateMultipartUploadRequest(bucketName, key);
var result = client.InitiateMultipartUpload(request); // 计算片个数
var fi = new FileInfo(fileToUpload);
var fileSize = fi.Length;
var partCount = fileSize / partSize;
if (fileSize % partSize != )
{
partCount++;
} // 开始分片上传
try
{
var partETags = new List<PartETag>();
using (var fs = File.Open(fileToUpload, FileMode.Open))
{
for (var i = ; i < partCount; i++)
{
var skipBytes = (long)partSize * i; //定位到本次上传片应该开始的位置
fs.Seek(skipBytes, ); //计算本次上传的片大小,最后一片为剩余的数据大小,其余片都是part size大小。
var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
var req = new UploadPartRequest(bucketName, filename, result.UploadId)
{
InputStream = fs,
PartSize = size,
PartNumber = i +
}; //调用UploadPart接口执行上传功能,返回结果中包含了这个数据片的ETag值
var ret = client.UploadPart(req);
partETags.Add(ret.PartETag);
}
Console.WriteLine("Put multi part upload succeeded : {0}", result.UploadId);
try
{
var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, result.UploadId);
foreach (var partETag in partETags)
{
completeMultipartUploadRequest.PartETags.Add(partETag);
}
var ret2 = client.CompleteMultipartUpload(completeMultipartUploadRequest);
Console.WriteLine("complete multi part succeeded");
}
catch (Exception ex)
{
Console.WriteLine("complete multi part failed, {0}", ex.Message);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Put multi part upload failed, {0}", ex.Message);
} }
catch (Exception ex)
{
Console.WriteLine("Init multi part upload failed, {0}", ex.Message);
}
}
}
}
}
05-21 14:38