1- using ImageResizeWebApp . Models ;
1+ using Azure . Storage ;
2+ using Azure . Storage . Blobs ;
3+ using Azure . Storage . Blobs . Models ;
4+ using ImageResizeWebApp . Models ;
25using Microsoft . AspNetCore . Http ;
3- using Microsoft . WindowsAzure . Storage ;
4- using Microsoft . WindowsAzure . Storage . Auth ;
5- using Microsoft . WindowsAzure . Storage . Blob ;
6-
76using System ;
87using System . Collections . Generic ;
98using System . IO ;
@@ -27,25 +26,26 @@ public static bool IsImage(IFormFile file)
2726 return formats . Any ( item => file . FileName . EndsWith ( item , StringComparison . OrdinalIgnoreCase ) ) ;
2827 }
2928
30- public static async Task < bool > UploadFileToStorage ( Stream fileStream , string fileName , AzureStorageConfig _storageConfig )
29+ public static async Task < bool > UploadFileToStorage ( Stream fileStream , string fileName ,
30+ AzureStorageConfig _storageConfig )
3131 {
32- // Create storagecredentials object by reading the values from the configuration (appsettings.json)
33- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
34-
35- // Create cloudstorage account by passing the storagecredentials
36- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
32+ // Create a URI to the blob
33+ Uri blobUri = new Uri ( "https://" +
34+ _storageConfig . AccountName +
35+ ".blob.core.windows.net/" +
36+ _storageConfig . ImageContainer +
37+ "/" + fileName ) ;
38+
39+ // Create StorageSharedKeyCredentials object by reading
40+ // the values from the configuration (appsettings.json)
41+ StorageSharedKeyCredential storageCredentials =
42+ new StorageSharedKeyCredential ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
3743
3844 // Create the blob client.
39- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
40-
41- // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
42- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ImageContainer ) ;
43-
44- // Get the reference to the block blob from the container
45- CloudBlockBlob blockBlob = container . GetBlockBlobReference ( fileName ) ;
45+ BlobClient blobClient = new BlobClient ( blobUri , storageCredentials ) ;
4646
4747 // Upload the file
48- await blockBlob . UploadFromStreamAsync ( fileStream ) ;
48+ await blobClient . UploadAsync ( fileStream ) ;
4949
5050 return await Task . FromResult ( true ) ;
5151 }
@@ -54,41 +54,23 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
5454 {
5555 List < string > thumbnailUrls = new List < string > ( ) ;
5656
57- // Create storagecredentials object by reading the values from the configuration (appsettings.json)
58- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
59-
60- // Create cloudstorage account by passing the storagecredentials
61- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
57+ // Create a URI to the storage account
58+ Uri accountUri = new Uri ( "https://" + _storageConfig . AccountName + ".blob.core.windows.net/" ) ;
6259
63- // Create blob client
64- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
60+ // Create BlobServiceClient from the account URI
61+ BlobServiceClient blobServiceClient = new BlobServiceClient ( accountUri ) ;
6562
6663 // Get reference to the container
67- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ThumbnailContainer ) ;
64+ BlobContainerClient container = blobServiceClient . GetBlobContainerClient ( _storageConfig . ThumbnailContainer ) ;
6865
69- BlobContinuationToken continuationToken = null ;
70-
71- BlobResultSegment resultSegment = null ;
72-
73- //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
74- //When the continuation token is null, the last page has been returned and execution can exit the loop.
75- do
66+ if ( container . Exists ( ) )
7667 {
77- //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
78- //or by calling a different overload.
79- resultSegment = await container . ListBlobsSegmentedAsync ( "" , true , BlobListingDetails . All , 10 , continuationToken , null , null ) ;
80-
81- foreach ( var blobItem in resultSegment . Results )
68+ foreach ( BlobItem blobItem in container . GetBlobs ( ) )
8269 {
83- thumbnailUrls . Add ( blobItem . StorageUri . PrimaryUri . ToString ( ) ) ;
70+ thumbnailUrls . Add ( container . Uri + "/" + blobItem . Name ) ;
8471 }
85-
86- //Get the continuation token.
87- continuationToken = resultSegment . ContinuationToken ;
8872 }
8973
90- while ( continuationToken != null ) ;
91-
9274 return await Task . FromResult ( thumbnailUrls ) ;
9375 }
9476 }
0 commit comments