1
1
using ImageResizeWebApp . Models ;
2
2
using Microsoft . AspNetCore . Http ;
3
- using Microsoft . WindowsAzure . Storage ;
4
- using Microsoft . WindowsAzure . Storage . Auth ;
5
- using Microsoft . WindowsAzure . Storage . Blob ;
3
+ using Azure . Storage ;
4
+ using Azure . Storage . Blobs ;
6
5
7
6
using System ;
8
7
using System . Collections . Generic ;
9
8
using System . IO ;
10
9
using System . Linq ;
11
10
using System . Threading . Tasks ;
11
+ using Azure . Storage . Blobs . Models ;
12
12
13
13
namespace ImageResizeWebApp . Helpers
14
14
{
@@ -29,23 +29,29 @@ public static bool IsImage(IFormFile file)
29
29
30
30
public static async Task < bool > UploadFileToStorage ( Stream fileStream , string fileName , AzureStorageConfig _storageConfig )
31
31
{
32
+ // Create a URI to the blob
33
+ Uri uri = new Uri ( "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" + _storageConfig . ImageContainer + "/" + fileName ) ;
34
+
32
35
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
33
- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
36
+ StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
34
37
35
38
// Create cloudstorage account by passing the storagecredentials
36
- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
39
+ //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
40
+ //BlobServiceClient blobServiceClient = new BlobServiceClient(uri);
37
41
38
42
// Create the blob client.
39
- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
43
+ BlobClient blobClient = new BlobClient ( uri , storageCredentials ) ;
40
44
41
45
// 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 ) ;
46
+ //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer);
47
+ //BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ImageContainer);
43
48
44
49
// Get the reference to the block blob from the container
45
- CloudBlockBlob blockBlob = container . GetBlockBlobReference ( fileName ) ;
50
+ // CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
46
51
47
52
// Upload the file
48
- await blockBlob . UploadFromStreamAsync ( fileStream ) ;
53
+ //await blockBlob.UploadFromStreamAsync(fileStream);
54
+ await blobClient . UploadAsync ( fileStream ) ;
49
55
50
56
return await Task . FromResult ( true ) ;
51
57
}
@@ -54,41 +60,51 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
54
60
{
55
61
List < string > thumbnailUrls = new List < string > ( ) ;
56
62
63
+ // Create a URI to the thumbnail container
64
+ Uri uri = new Uri ( "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" ) ;
65
+
57
66
// Create storagecredentials object by reading the values from the configuration (appsettings.json)
58
- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
67
+ //StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential (_storageConfig.AccountName, _storageConfig.AccountKey);
59
68
60
69
// Create cloudstorage account by passing the storagecredentials
61
- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
70
+ //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
71
+ BlobServiceClient blobServiceClient = new BlobServiceClient ( uri ) ;
62
72
63
73
// Create blob client
64
- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
74
+ //BlobClient blobClient = new BlobClient(uri, storageCredentials );
65
75
66
76
// Get reference to the container
67
- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ThumbnailContainer ) ;
77
+ //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
78
+ BlobContainerClient container = await blobServiceClient . CreateBlobContainerAsync ( _storageConfig . ThumbnailContainer ) ;
68
79
69
- BlobContinuationToken continuationToken = null ;
80
+ // BlobContinuationToken continuationToken = null;
70
81
71
- BlobResultSegment resultSegment = null ;
82
+ // BlobResultSegment resultSegment = null;
72
83
73
84
//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
74
85
//When the continuation token is null, the last page has been returned and execution can exit the loop.
75
- do
86
+ //do
87
+ //{
88
+ // //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
89
+ // //or by calling a different overload.
90
+ // resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
91
+
92
+ // foreach (var blobItem in resultSegment.Results)
93
+ // {
94
+ // thumbnailUrls.Add(blobItem.StorageUri.PrimaryUri.ToString());
95
+ // }
96
+
97
+ // //Get the continuation token.
98
+ // continuationToken = resultSegment.ContinuationToken;
99
+ //}
100
+ //while (continuationToken != null);
101
+
102
+ foreach ( BlobItem blobItem in container . GetBlobs ( ) )
76
103
{
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 )
82
- {
83
- thumbnailUrls . Add ( blobItem . StorageUri . PrimaryUri . ToString ( ) ) ;
84
- }
85
-
86
- //Get the continuation token.
87
- continuationToken = resultSegment . ContinuationToken ;
104
+ string thumbnailUrl = "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" + _storageConfig . ThumbnailContainer + "/" + blobItem . Name ;
105
+ thumbnailUrls . Add ( thumbnailUrl ) ;
88
106
}
89
107
90
- while ( continuationToken != null ) ;
91
-
92
108
return await Task . FromResult ( thumbnailUrls ) ;
93
109
}
94
110
}
0 commit comments