1
- using ImageResizeWebApp . Models ;
1
+ using Azure . Storage ;
2
+ using Azure . Storage . Blobs ;
3
+ using Azure . Storage . Blobs . Models ;
4
+ using ImageResizeWebApp . Models ;
2
5
using Microsoft . AspNetCore . Http ;
3
- using Microsoft . WindowsAzure . Storage ;
4
- using Microsoft . WindowsAzure . Storage . Auth ;
5
- using Microsoft . WindowsAzure . Storage . Blob ;
6
-
7
6
using System ;
8
7
using System . Collections . Generic ;
9
8
using System . IO ;
@@ -27,25 +26,26 @@ public static bool IsImage(IFormFile file)
27
26
return formats . Any ( item => file . FileName . EndsWith ( item , StringComparison . OrdinalIgnoreCase ) ) ;
28
27
}
29
28
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 )
31
31
{
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 ) ;
37
43
38
44
// 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 ) ;
46
46
47
47
// Upload the file
48
- await blockBlob . UploadFromStreamAsync ( fileStream ) ;
48
+ await blobClient . UploadAsync ( fileStream ) ;
49
49
50
50
return await Task . FromResult ( true ) ;
51
51
}
@@ -54,41 +54,23 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
54
54
{
55
55
List < string > thumbnailUrls = new List < string > ( ) ;
56
56
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/" ) ;
62
59
63
- // Create blob client
64
- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
60
+ // Create BlobServiceClient from the account URI
61
+ BlobServiceClient blobServiceClient = new BlobServiceClient ( accountUri ) ;
65
62
66
63
// Get reference to the container
67
- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ThumbnailContainer ) ;
64
+ BlobContainerClient container = blobServiceClient . GetBlobContainerClient ( _storageConfig . ThumbnailContainer ) ;
68
65
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 ( ) )
76
67
{
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 ( ) )
82
69
{
83
- thumbnailUrls . Add ( blobItem . StorageUri . PrimaryUri . ToString ( ) ) ;
70
+ thumbnailUrls . Add ( container . Uri + "/" + blobItem . Name ) ;
84
71
}
85
-
86
- //Get the continuation token.
87
- continuationToken = resultSegment . ContinuationToken ;
88
72
}
89
73
90
- while ( continuationToken != null ) ;
91
-
92
74
return await Task . FromResult ( thumbnailUrls ) ;
93
75
}
94
76
}
0 commit comments