1
- using System ;
1
+ using Azure . Storage . Blobs ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
4
using System . IO ;
4
5
using System . Linq ;
5
- using System . Reflection ;
6
- using Azure . Core . Pipeline ;
7
- using Azure . Storage ;
8
- using Azure . Storage . Blobs ;
9
- using Azure . Storage . Blobs . Models ;
10
6
11
7
namespace Microsoft . SourceBrowser . SourceIndexServer . Models
12
8
{
13
9
public class AzureBlobFileSystem : IFileSystem
14
10
{
15
- private class AnonContainerClient : BlobContainerClient
16
- {
17
- public override Uri Uri { get ; }
18
- protected override HttpPipeline Pipeline { get ; }
19
-
20
- public AnonContainerClient ( string uri )
21
- {
22
- Uri = new Uri ( uri ) ;
23
- Pipeline = HttpPipelineBuilder . Build ( new BlobClientOptions ( ) ) ;
24
- }
25
-
26
- public override BlobClient GetBlobClient ( string blobName )
27
- {
28
- return ( BlobClient ) typeof ( BlobClient ) . GetTypeInfo ( ) . DeclaredConstructors . Single ( ctor => ctor . ToString ( ) == "Void .ctor(System.Uri, Azure.Core.Pipeline.HttpPipeline)" ) . Invoke ( new object [ ] { AppendToPath ( this . Uri , blobName ) , this . Pipeline } ) ;
29
- }
30
-
31
- private static Uri AppendToPath ( Uri uri , string segment )
32
- {
33
- UriBuilder uriBuilder = new UriBuilder ( uri ) ;
34
- string path = uriBuilder . Path ;
35
- uriBuilder . Path = uriBuilder . Path + ( path . Length == 0 || path [ path . Length - 1 ] != '/' ? "/" : "" ) + segment ;
36
- return uriBuilder . Uri ;
37
- }
38
- }
39
-
40
11
private readonly BlobContainerClient container ;
12
+
41
13
public AzureBlobFileSystem ( string uri )
42
14
{
43
- container = new AnonContainerClient ( uri ) ;
15
+ container = new BlobContainerClient ( new Uri ( uri ) ) ;
44
16
}
45
17
46
18
public bool DirectoryExists ( string name )
@@ -57,45 +29,37 @@ public IEnumerable<string> ListFiles(string dirName)
57
29
dirName += "/" ;
58
30
}
59
31
60
- return container . GetBlobsByHierarchy ( "/" , new GetBlobsOptions
61
- {
62
- Prefix = dirName ,
63
- } ) . Select ( res => res . Value ) . Where ( item => item . IsBlob ) . Select ( item => item . Blob . Name ) . ToList ( ) ;
32
+ return container . GetBlobsByHierarchy ( prefix : dirName )
33
+ . Where ( item => item . IsBlob )
34
+ . Select ( item => item . Blob . Name )
35
+ . ToList ( ) ;
64
36
}
65
37
66
38
public bool FileExists ( string name )
67
39
{
68
40
name = name . ToLowerInvariant ( ) ;
69
- var blob = container . GetBlobClient ( name ) ;
70
- try
71
- {
72
- blob . GetProperties ( ) ;
73
- return true ;
74
- }
75
- catch ( StorageRequestFailedException ex ) when ( string . Equals ( ex . ErrorCode , "BlobNotFound" ) )
76
- {
77
- return false ;
78
- }
41
+ BlobClient blob = container . GetBlobClient ( name ) ;
42
+
43
+ return blob . Exists ( ) ;
79
44
}
80
45
81
46
public Stream OpenSequentialReadStream ( string name )
82
47
{
83
48
name = name . ToLowerInvariant ( ) ;
84
- var blob = container . GetBlobClient ( name ) ;
85
- return blob . Download ( ) . Value . Content ;
49
+ BlobClient blob = container . GetBlobClient ( name ) ;
50
+ return blob . OpenRead ( ) ;
86
51
}
87
52
88
53
public IEnumerable < string > ReadLines ( string name )
89
54
{
90
55
name = name . ToLowerInvariant ( ) ;
91
- var blob = container . GetBlobClient ( name ) ;
92
- using ( var stream = blob . Download ( ) . Value . Content )
93
- using ( var reader = new StreamReader ( stream ) )
56
+ BlobClient blob = container . GetBlobClient ( name ) ;
57
+ using Stream stream = blob . OpenRead ( ) ;
58
+ using StreamReader reader = new ( stream ) ;
59
+
60
+ while ( ! reader . EndOfStream )
94
61
{
95
- while ( ! reader . EndOfStream )
96
- {
97
- yield return reader . ReadLine ( ) ;
98
- }
62
+ yield return reader . ReadLine ( ) ;
99
63
}
100
64
}
101
65
}
0 commit comments