1- using ManagedCode . Storage . Core ;
1+ using Azure . Storage . Blobs ;
2+ using Azure . Storage . Blobs . Models ;
3+ using ManagedCode . Storage . Azure . Options ;
4+ using ManagedCode . Storage . Core ;
25using ManagedCode . Storage . Core . Models ;
3- using System ;
46using System . Collections . Generic ;
57using System . IO ;
8+ using System . Runtime . CompilerServices ;
69using System . Threading ;
710using System . Threading . Tasks ;
811
912namespace ManagedCode . Storage . Azure
1013{
1114 public class AzureBlobStorage : IBlobStorage
1215 {
16+ private readonly BlobContainerClient _blobContainerClient ;
17+
1318 public AzureBlobStorage ( AzureBlobStorageConnectionOptions connectionOptions )
1419 {
15- // var blobServiceClient = new BlobServiceClient(connectionOptions.ConnectionString);
16- }
20+ _blobContainerClient = new BlobContainerClient (
21+ connectionOptions . ConnectionString ,
22+ connectionOptions . Container
23+ ) ;
1724
18- public Task DeleteAsync ( string blob , CancellationToken cancellationToken = default )
19- {
20- throw new System . NotImplementedException ( ) ;
25+ _blobContainerClient . CreateIfNotExists ( PublicAccessType . BlobContainer ) ;
2126 }
2227
23- public Task DeleteAsync ( Blob blob , CancellationToken cancellationToken = default )
28+ public void Dispose ( )
2429 {
25- throw new System . NotImplementedException ( ) ;
2630 }
2731
28- public Task DeleteAsync ( IEnumerable < string > blobs , CancellationToken cancellationToken = default )
29- {
30- throw new System . NotImplementedException ( ) ;
31- }
32+ #region Delete
3233
33- public Task DeleteAsync ( IEnumerable < Blob > blobs , CancellationToken cancellationToken = default )
34+ public async Task DeleteAsync ( string blob , CancellationToken cancellationToken = default )
3435 {
35- throw new System . NotImplementedException ( ) ;
36+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
37+ await blobClient . DeleteAsync ( DeleteSnapshotsOption . None , null , cancellationToken ) ;
3638 }
3739
38- public void Dispose ( )
40+ public async Task DeleteAsync ( Blob blob , CancellationToken cancellationToken = default )
3941 {
40- throw new System . NotImplementedException ( ) ;
42+ var blobClient = _blobContainerClient . GetBlobClient ( blob . Name ) ;
43+ await blobClient . DeleteAsync ( DeleteSnapshotsOption . None , null , cancellationToken ) ;
4144 }
4245
43- public Task < Stream > DownloadAsStreamAsync ( string blob , CancellationToken cancellationToken = default )
46+ public async Task DeleteAsync ( IEnumerable < string > blobs , CancellationToken cancellationToken = default )
4447 {
45- throw new System . NotImplementedException ( ) ;
48+ foreach ( var blobName in blobs )
49+ {
50+ await DeleteAsync ( blobName , cancellationToken ) ;
51+ }
4652 }
4753
48- public Task < Stream > DownloadAsStreamAsync ( Blob blob , CancellationToken cancellationToken = default )
54+ public async Task DeleteAsync ( IEnumerable < Blob > blobs , CancellationToken cancellationToken = default )
4955 {
50- throw new System . NotImplementedException ( ) ;
56+ foreach ( var blob in blobs )
57+ {
58+ await DeleteAsync ( blob , cancellationToken ) ;
59+ }
5160 }
5261
53- public Task < LocalFile > DownloadAsync ( string blob , CancellationToken cancellationToken = default )
62+ #endregion
63+
64+ public async Task < Stream > DownloadAsStreamAsync ( string blob , CancellationToken cancellationToken = default )
5465 {
55- throw new System . NotImplementedException ( ) ;
66+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
67+ var res = await blobClient . DownloadStreamingAsync ( ) ;
68+
69+ return res . Value . Content ;
5670 }
5771
58- public Task < LocalFile > DownloadAsync ( Blob blob , CancellationToken cancellationToken = default )
72+ public async Task < Stream > DownloadAsStreamAsync ( Blob blob , CancellationToken cancellationToken = default )
5973 {
60- throw new System . NotImplementedException ( ) ;
74+ return await DownloadAsStreamAsync ( blob . Name , cancellationToken ) ;
6175 }
6276
63- public Task < bool > ExistsAsync ( string blob , CancellationToken cancellationToken = default )
77+ public async Task < LocalFile > DownloadAsync ( string blob , CancellationToken cancellationToken = default )
6478 {
65- throw new System . NotImplementedException ( ) ;
79+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
80+ var localFile = new LocalFile ( ) ;
81+
82+ await blobClient . DownloadToAsync ( localFile . FileStream , cancellationToken ) ;
83+
84+ return localFile ;
6685 }
6786
68- public Task < bool > ExistsAsync ( Blob blob , CancellationToken cancellationToken = default )
87+ public async Task < LocalFile > DownloadAsync ( Blob blob , CancellationToken cancellationToken = default )
6988 {
70- throw new System . NotImplementedException ( ) ;
89+ return await DownloadAsync ( blob . Name , cancellationToken ) ;
7190 }
7291
73- public IAsyncEnumerable < bool > ExistsAsync ( IEnumerable < string > blobs , CancellationToken cancellationToken = default )
92+ #region Exists
93+
94+ public async Task < bool > ExistsAsync ( string blob , CancellationToken cancellationToken = default )
7495 {
75- throw new System . NotImplementedException ( ) ;
96+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
97+
98+ return await blobClient . ExistsAsync ( cancellationToken ) ;
7699 }
77100
78- public IAsyncEnumerable < bool > ExistsAsync ( IEnumerable < Blob > blobs , CancellationToken cancellationToken = default )
101+ public async Task < bool > ExistsAsync ( Blob blob , CancellationToken cancellationToken = default )
79102 {
80- throw new System . NotImplementedException ( ) ;
103+ var blobClient = _blobContainerClient . GetBlobClient ( blob . Name ) ;
104+
105+ return await blobClient . ExistsAsync ( cancellationToken ) ;
81106 }
82107
83- public IAsyncEnumerable < Blob > GetBlob ( string blob , CancellationToken cancellationToken = default )
108+ public async IAsyncEnumerable < bool > ExistsAsync ( IEnumerable < string > blobs ,
109+ [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
84110 {
85- throw new System . NotImplementedException ( ) ;
111+ foreach ( var blob in blobs )
112+ {
113+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
114+ yield return await blobClient . ExistsAsync ( cancellationToken ) ;
115+ }
86116 }
87117
88- public IAsyncEnumerable < Blob > GetBlob ( Blob blob , CancellationToken cancellationToken = default )
118+ public async IAsyncEnumerable < bool > ExistsAsync ( IEnumerable < Blob > blobs ,
119+ [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
89120 {
90- throw new System . NotImplementedException ( ) ;
121+ foreach ( var blob in blobs )
122+ {
123+ var blobClient = _blobContainerClient . GetBlobClient ( blob . Name ) ;
124+ yield return await blobClient . ExistsAsync ( cancellationToken ) ;
125+ }
91126 }
92127
93- public IAsyncEnumerable < Blob > GetBlob ( IEnumerable < string > blobs , CancellationToken cancellationToken = default )
128+ #endregion
129+
130+ public async Task < Blob > GetBlobAsync ( string blob , CancellationToken cancellationToken = default )
94131 {
95- throw new System . NotImplementedException ( ) ;
132+ await Task . Yield ( ) ;
133+
134+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
135+
136+ return new Blob ( )
137+ {
138+ Name = blobClient . Name ,
139+ Uri = blobClient . Uri
140+ } ;
96141 }
97142
98- public IAsyncEnumerable < Blob > GetBlob ( IEnumerable < Blob > blobs , CancellationToken cancellationToken = default )
143+ public IAsyncEnumerable < Blob > GetBlobsAsync ( IEnumerable < string > blobs , CancellationToken cancellationToken = default )
99144 {
100145 throw new System . NotImplementedException ( ) ;
101146 }
@@ -105,24 +150,34 @@ public IAsyncEnumerable<Blob> GetBlobListAsync(CancellationToken cancellationTok
105150 throw new System . NotImplementedException ( ) ;
106151 }
107152
108- public Task UploadAsync ( string blob , Stream dataStream , bool append = false , CancellationToken cancellationToken = default )
153+ #region Upload
154+
155+ public async Task UploadAsync ( string blob , Stream dataStream , bool append = false , CancellationToken cancellationToken = default )
109156 {
110- throw new System . NotImplementedException ( ) ;
157+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
158+ await blobClient . UploadAsync ( dataStream , cancellationToken ) ;
111159 }
112160
113- public Task UploadAsync ( string blob , string pathToFile , bool append = false , CancellationToken cancellationToken = default )
161+ public async Task UploadAsync ( string blob , string pathToFile , bool append = false , CancellationToken cancellationToken = default )
114162 {
115- throw new System . NotImplementedException ( ) ;
163+ var blobClient = _blobContainerClient . GetBlobClient ( blob ) ;
164+
165+ using ( var fs = new FileStream ( pathToFile , FileMode . Open , FileAccess . Read ) )
166+ {
167+ await blobClient . UploadAsync ( fs , cancellationToken ) ;
168+ }
116169 }
117170
118- public Task UploadAsync ( Blob blob , Stream dataStream , bool append = false , CancellationToken cancellationToken = default )
171+ public async Task UploadAsync ( Blob blob , Stream dataStream , bool append = false , CancellationToken cancellationToken = default )
119172 {
120- throw new System . NotImplementedException ( ) ;
173+ await UploadAsync ( blob . Name , dataStream , append , cancellationToken ) ;
121174 }
122175
123- public Task UploadAsync ( Blob blob , string pathToFile , bool append = false , CancellationToken cancellationToken = default )
176+ public async Task UploadAsync ( Blob blob , string pathToFile , bool append = false , CancellationToken cancellationToken = default )
124177 {
125- throw new System . NotImplementedException ( ) ;
178+ await UploadAsync ( blob . Name , pathToFile , append , cancellationToken ) ;
126179 }
180+
181+ #endregion
127182 }
128183}
0 commit comments