@@ -30,27 +30,21 @@ public static bool IsImage(IFormFile file)
30
30
public static async Task < bool > UploadFileToStorage ( Stream fileStream , string fileName , AzureStorageConfig _storageConfig )
31
31
{
32
32
// Create a URI to the blob
33
- Uri uri = new Uri ( "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" + _storageConfig . ImageContainer + "/" + fileName ) ;
33
+ Uri blobUri = new Uri ( "https://" +
34
+ _storageConfig . AccountName +
35
+ ".blob.core.windows.net/" +
36
+ _storageConfig . ImageContainer +
37
+ "/" + fileName ) ;
34
38
35
- // Create storagecredentials object by reading the values from the configuration (appsettings.json)
36
- StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
37
-
38
- // Create cloudstorage account by passing the storagecredentials
39
- //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
40
- //BlobServiceClient blobServiceClient = new BlobServiceClient(uri);
39
+ // Create StorageSharedKeyCredentials object by reading
40
+ // the values from the configuration (appsettings.json)
41
+ StorageSharedKeyCredential storageCredentials =
42
+ new StorageSharedKeyCredential ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
41
43
42
44
// Create the blob client.
43
- BlobClient blobClient = new BlobClient ( uri , storageCredentials ) ;
44
-
45
- // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
46
- //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ImageContainer);
47
- //BlobContainerClient container = await blobServiceClient.CreateBlobContainerAsync(_storageConfig.ImageContainer);
48
-
49
- // Get the reference to the block blob from the container
50
- //CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
45
+ BlobClient blobClient = new BlobClient ( blobUri , storageCredentials ) ;
51
46
52
47
// Upload the file
53
- //await blockBlob.UploadFromStreamAsync(fileStream);
54
48
await blobClient . UploadAsync ( fileStream ) ;
55
49
56
50
return await Task . FromResult ( true ) ;
@@ -60,49 +54,18 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
60
54
{
61
55
List < string > thumbnailUrls = new List < string > ( ) ;
62
56
63
- // Create a URI to the thumbnail container
64
- Uri uri = new Uri ( "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" ) ;
65
-
66
- // Create storagecredentials object by reading the values from the configuration (appsettings.json)
67
- //StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
68
-
69
- // Create cloudstorage account by passing the storagecredentials
70
- //CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
71
- BlobServiceClient blobServiceClient = new BlobServiceClient ( uri ) ;
57
+ // Create a URI to the storage account
58
+ Uri accountUri = new Uri ( "https://" + _storageConfig . AccountName + ".blob.core.windows.net/" ) ;
72
59
73
- // Create blob client
74
- //BlobClient blobClient = new BlobClient(uri, storageCredentials );
60
+ // Create BlobServiceClient from the account URI
61
+ BlobServiceClient blobServiceClient = new BlobServiceClient ( accountUri ) ;
75
62
76
63
// Get reference to the container
77
- //CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.ThumbnailContainer);
78
- BlobContainerClient container = await blobServiceClient . CreateBlobContainerAsync ( _storageConfig . ThumbnailContainer ) ;
79
-
80
- //BlobContinuationToken continuationToken = null;
81
-
82
- //BlobResultSegment resultSegment = null;
83
-
84
- //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
85
- //When the continuation token is null, the last page has been returned and execution can exit the loop.
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);
64
+ BlobContainerClient container = blobServiceClient . GetBlobContainerClient ( _storageConfig . ThumbnailContainer ) ;
101
65
102
66
foreach ( BlobItem blobItem in container . GetBlobs ( ) )
103
67
{
104
- string thumbnailUrl = "https://" + _storageConfig . AccountName + "/.blob.core.windows.net/" + _storageConfig . ThumbnailContainer + "/" + blobItem . Name ;
105
- thumbnailUrls . Add ( thumbnailUrl ) ;
68
+ thumbnailUrls . Add ( container . Uri + "/" + blobItem . Name ) ;
106
69
}
107
70
108
71
return await Task . FromResult ( thumbnailUrls ) ;
0 commit comments