55
66import { type FunctionsDeploymentStorageAuthentication , type NameValuePair , type Site , type SiteConfig , type WebSiteManagementClient } from '@azure/arm-appservice' ;
77import { type Identity } from '@azure/arm-resources' ;
8+ import { type StorageAccount } from '@azure/arm-storage' ;
89import { BlobServiceClient } from '@azure/storage-blob' ;
910import { ParsedSite , WebsiteOS , type CustomLocation , type IAppServiceWizardContext } from '@microsoft/vscode-azext-azureappservice' ;
1011import { 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' ;
1213import { type AppResource } from '@microsoft/vscode-azext-utils/hostapi' ;
1314import { type Progress } from 'vscode' ;
1415import { FuncVersion , getMajorVersion } from '../../FuncVersion' ;
@@ -18,7 +19,6 @@ import { localize } from '../../localize';
1819import { createWebSiteClient } from '../../utils/azureClients' ;
1920import { getRandomHexString } from '../../utils/fs' ;
2021import { createAzureWebJobsStorageManagedIdentitySettings } from '../../utils/managedIdentityUtils' ;
21- import { nonNullProp } from '../../utils/nonNull' ;
2222import { getStorageConnectionString } from '../appSettings/connectionSettings/getLocalConnectionSetting' ;
2323import { enableFileLogging } from '../logstream/enableFileLogging' ;
2424import { type FullFunctionAppStack , type IFlexFunctionAppWizardContext , type IFunctionAppWizardContext } from './IFunctionAppWizardContext' ;
@@ -241,8 +241,9 @@ export class FunctionAppCreateStep extends AzureWizardExecuteStepWithActivityOut
241241 const result = await client . webApps . beginCreateOrUpdateAndWait ( rgName , siteName , site ) ;
242242
243243 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+ }
246247 }
247248
248249 return result ;
@@ -284,16 +285,28 @@ function getSiteKind(context: IAppServiceWizardContext): string {
284285}
285286
286287// 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+
288301 try {
289- const blobClient = BlobServiceClient . fromConnectionString ( storageConnectionString ) ;
290302 const containerUrl : string | undefined = site . functionAppConfig ?. deployment ?. storage ?. value ;
291303 if ( containerUrl ) {
292304 const containerName = containerUrl . split ( '/' ) . pop ( ) ;
293305 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
297310 } else {
298311 ext . outputChannel . appendLog ( localize ( 'deploymentStorageExists' , 'Deployment storage container "{0}" already exists.' , containerName ) ) ;
299312 return ;
0 commit comments