1
1
using System ;
2
+ using System . IO ;
2
3
using System . Threading . Tasks ;
3
- using Sample ;
4
+ using Cuiliang . AliyunOssSdk ;
5
+ using Cuiliang . AliyunOssSdk . Entites ;
6
+ using Cuiliang . AliyunOssSdk . Request ;
7
+ using Microsoft . Extensions . Configuration ;
8
+ using Microsoft . Extensions . DependencyInjection ;
4
9
5
10
class Program
6
11
{
7
- public static void Main ( string [ ] args )
12
+ public static async Task Main ( string [ ] args )
8
13
{
9
- MainAsync ( args ) . GetAwaiter ( ) . GetResult ( ) ;
10
-
11
- Console . ReadKey ( ) ;
12
- }
13
-
14
- public static async Task MainAsync ( string [ ] args )
15
- {
16
- try
17
- {
18
- await NewSdkTester . RunAsync ( ) ;
19
- }
20
- catch ( Exception ex )
14
+ if ( args . Length < 2 )
21
15
{
22
- Console . WriteLine ( "Ex:" + ex . Message ) ;
23
- throw ;
16
+ Console . WriteLine ( "Please run with bucket name and file path" ) ;
17
+ return ;
24
18
}
25
19
20
+ var bucketName = args [ 0 ] ;
21
+ var filePath = args [ 1 ] ;
26
22
23
+ var conf = new ConfigurationBuilder ( )
24
+ . SetBasePath ( Directory . GetCurrentDirectory ( ) )
25
+ . AddJsonFile ( "appsettings.json" , true , true )
26
+ . AddJsonFile ( "appsettings.Development.json" , true , true )
27
+ . Build ( ) ;
28
+
29
+ IServiceCollection services = new ServiceCollection ( ) ;
30
+ services . AddOssClient ( conf ) ;
31
+ var sp = services . BuildServiceProvider ( ) ;
32
+ var client = sp . GetService < OssClient > ( ) ;
33
+
34
+ var bucket = BucketInfo . CreateByRegion ( OssRegions . HangZhou , bucketName , false , false ) ;
35
+ var remoteFilePaht = $ "test/{ DateTime . Now . ToString ( "yyyyMMddHHmmss" ) } .png";
36
+ using ( var fs = new FileStream ( filePath , FileMode . Open ) )
37
+ {
38
+ var file = new RequestContent ( )
39
+ {
40
+ MimeType = "images/png" ,
41
+ ContentType = RequestContentType . Stream ,
42
+ StreamContent = fs ,
43
+ } ;
44
+
45
+ var result = await client . PutObjectAsync ( bucket , remoteFilePaht , file ) ;
46
+ if ( result . IsSuccess )
47
+ {
48
+ Console . WriteLine ( "Succeeded!" ) ;
49
+ }
50
+ else
51
+ {
52
+ Console . WriteLine ( "Failed!" ) ;
53
+ Console . WriteLine ( result . ErrorMessage ) ;
54
+ }
55
+ }
27
56
}
28
57
}
0 commit comments