5
5
6
6
import { type FunctionsDeploymentStorageAuthentication , type NameValuePair , type Site , type SiteConfig , type WebSiteManagementClient } from '@azure/arm-appservice' ;
7
7
import { type Identity } from '@azure/arm-resources' ;
8
+ import { type StorageAccount } from '@azure/arm-storage' ;
8
9
import { BlobServiceClient } from '@azure/storage-blob' ;
9
10
import { ParsedSite , WebsiteOS , type CustomLocation , type IAppServiceWizardContext } from '@microsoft/vscode-azext-azureappservice' ;
10
11
import { LocationListStep } from '@microsoft/vscode-azext-azureutils' ;
11
- import { AzureWizardExecuteStepWithActivityOutput , maskUserInfo , parseError , randomUtils } from '@microsoft/vscode-azext-utils' ;
12
+ import { AzureWizardExecuteStepWithActivityOutput , maskUserInfo , nonNullProp , parseError , randomUtils } from '@microsoft/vscode-azext-utils' ;
12
13
import { type AppResource } from '@microsoft/vscode-azext-utils/hostapi' ;
13
14
import { type Progress } from 'vscode' ;
14
15
import { FuncVersion , getMajorVersion } from '../../FuncVersion' ;
@@ -18,7 +19,6 @@ import { localize } from '../../localize';
18
19
import { createWebSiteClient } from '../../utils/azureClients' ;
19
20
import { getRandomHexString } from '../../utils/fs' ;
20
21
import { createAzureWebJobsStorageManagedIdentitySettings } from '../../utils/managedIdentityUtils' ;
21
- import { nonNullProp } from '../../utils/nonNull' ;
22
22
import { getStorageConnectionString } from '../appSettings/connectionSettings/getLocalConnectionSetting' ;
23
23
import { enableFileLogging } from '../logstream/enableFileLogging' ;
24
24
import { type FullFunctionAppStack , type IFlexFunctionAppWizardContext , type IFunctionAppWizardContext } from './IFunctionAppWizardContext' ;
@@ -241,8 +241,9 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStepWithActivityOut
241
241
const result = await client . webApps . beginCreateOrUpdateAndWait ( rgName , siteName , site ) ;
242
242
243
243
if ( context . newFlexSku ) {
244
- const storageConnectionString : string = ( await getStorageConnectionString ( context ) ) . connectionString ;
245
- await tryCreateStorageContainer ( result , storageConnectionString ) ;
244
+ if ( context . storageAccount ) {
245
+ await tryCreateStorageContainer ( context , result , context . storageAccount ) ;
246
+ }
246
247
}
247
248
248
249
return result ;
@@ -284,16 +285,28 @@ function getSiteKind(context: IAppServiceWizardContext): string {
284
285
}
285
286
286
287
// storage container is needed for flex deployment, but it is not created automatically
287
- async function tryCreateStorageContainer ( site : Site , storageConnectionString : string ) : Promise < void > {
288
+ async function tryCreateStorageContainer ( context : IFlexFunctionAppWizardContext , site : Site , storageAccount : StorageAccount ) : Promise < void > {
289
+ let client : BlobServiceClient ;
290
+ try {
291
+ const token = await context . createCredentialsForScopes ( [ 'https://storage.azure.com/.default' ] )
292
+ const primaryEndpoint = nonNullProp ( storageAccount , 'primaryEndpoints' ) ;
293
+ client = new BlobServiceClient ( nonNullProp ( primaryEndpoint , 'blob' ) , token ) ;
294
+ await client . getProperties ( ) ; // Trigger a request to validate the token
295
+ } catch ( error ) {
296
+ const storageConnectionString : string = ( await getStorageConnectionString ( context ) ) . connectionString ;
297
+ client = BlobServiceClient . fromConnectionString ( storageConnectionString ) ;
298
+ await client . getProperties ( ) ; // Trigger a request to validate the key
299
+ }
300
+
288
301
try {
289
- const blobClient = BlobServiceClient . fromConnectionString ( storageConnectionString ) ;
290
302
const containerUrl : string | undefined = site . functionAppConfig ?. deployment ?. storage ?. value ;
291
303
if ( containerUrl ) {
292
304
const containerName = containerUrl . split ( '/' ) . pop ( ) ;
293
305
if ( containerName ) {
294
- const client = blobClient . getContainerClient ( containerName ) ;
295
- if ( ! await client . exists ( ) ) {
296
- await blobClient . createContainer ( containerName ) ;
306
+ const containerClient = client . getContainerClient ( containerName ) ;
307
+ if ( ! await containerClient . exists ( ) ) {
308
+ await client . createContainer ( containerName ) ;
309
+ return
297
310
} else {
298
311
ext . outputChannel . appendLog ( localize ( 'deploymentStorageExists' , 'Deployment storage container "{0}" already exists.' , containerName ) ) ;
299
312
return ;
0 commit comments