-
Notifications
You must be signed in to change notification settings - Fork 204
/
ResumableSample.cs
111 lines (103 loc) · 4.2 KB
/
ResumableSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
*/
using System;
using System.IO;
using System.Threading;
using Aliyun.OSS.Common;
using System.Text;
using Aliyun.OSS.Util;
namespace Aliyun.OSS.Samples
{
/// <summary>
/// Sample for resumbale upload and copy.
/// </summary>
public static class ResumbaleSample
{
static string accessKeyId = Config.AccessKeyId;
static string accessKeySecret = Config.AccessKeySecret;
static string endpoint = Config.Endpoint;
static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
static string fileToUpload = Config.BigFileToUpload;
private static void streamProgressCallback(object sender, StreamTransferProgressArgs args)
{
Console.WriteLine("ProgressCallback - TotalBytes:{0}, TransferredBytes:{1}, IncrementTransferred:{2}",
args.TotalBytes, args.TransferredBytes, args.IncrementTransferred);
}
public static void ResumableUploadObject(string bucketName)
{
const string key = "ResumableUploadObject";
string checkpointDir = Config.DirToDownload;
try
{
UploadObjectRequest request = new UploadObjectRequest(bucketName, key, fileToUpload)
{
PartSize = 8 * 1024 * 1024,
ParallelThreadCount = 3,
CheckpointDir = checkpointDir,
StreamTransferProgress = streamProgressCallback,
};
client.ResumableUploadObject(request);
Console.WriteLine("Resumable upload object:{0} succeeded", key);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
public static void ResumableDownloadObject(string bucketName)
{
const string key = "ResumableDownloadObject";
string fileToDownload = Config.DirToDownload + key;
string checkpointDir = Config.DirToDownload;
try
{
DownloadObjectRequest request = new DownloadObjectRequest(bucketName, key, fileToDownload)
{
PartSize = 8 * 1024 * 1024,
ParallelThreadCount = 3,
CheckpointDir = Config.DirToDownload,
StreamTransferProgress = streamProgressCallback,
};
client.ResumableDownloadObject(request);
Console.WriteLine("Resumable download object:{0} succeeded", key);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
public static void ResumableCopyObject(string sourceBucketName, string sourceKey,
string destBucketName, string destKey)
{
string checkpointDir = Config.DirToDownload;
try
{
var request = new CopyObjectRequest(sourceBucketName, sourceKey, destBucketName, destKey);
client.ResumableCopyObject(request, checkpointDir);
Console.WriteLine("Resumable copy new object:{0} succeeded", request.DestinationKey);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}