diff --git a/packages/storage-azure/src/index.ts b/packages/storage-azure/src/index.ts index cd1c4bd74e..105d0a2603 100644 --- a/packages/storage-azure/src/index.ts +++ b/packages/storage-azure/src/index.ts @@ -1,4 +1,3 @@ -import type { ContainerClient } from '@azure/storage-blob' import type { Adapter, PluginOptions as CloudStoragePluginOptions, @@ -7,13 +6,13 @@ import type { } from '@payloadcms/plugin-cloud-storage/types' import type { Config, Plugin } from 'payload' -import { BlobServiceClient } from '@azure/storage-blob' import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage' import { getGenerateURL } from './generateURL.js' import { getHandleDelete } from './handleDelete.js' import { getHandleUpload } from './handleUpload.js' import { getHandler } from './staticHandler.js' +import { getStorageClient as getStorageClientFunc } from './utils/getStorageClient.js' export type AzureStorageOptions = { /** @@ -105,19 +104,14 @@ function azureStorageInternal({ connectionString, containerName, }: AzureStorageOptions): Adapter { - let storageClient: ContainerClient | null = null - const getStorageClient = () => { - if (storageClient) return storageClient - - const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString) - storageClient = blobServiceClient.getContainerClient(containerName) - return storageClient - } - const createContainerIfNotExists = () => { - void getStorageClient().createIfNotExists({ access: 'blob' }) + void getStorageClientFunc({ connectionString, containerName }).createIfNotExists({ + access: 'blob', + }) } + const getStorageClient = () => getStorageClientFunc({ connectionString, containerName }) + return ({ collection, prefix }): GeneratedAdapter => { return { name: 'azure', @@ -133,3 +127,5 @@ function azureStorageInternal({ } } } + +export { getStorageClientFunc as getStorageClient } diff --git a/packages/storage-azure/src/utils/getStorageClient.ts b/packages/storage-azure/src/utils/getStorageClient.ts new file mode 100644 index 0000000000..8d68a7c7d5 --- /dev/null +++ b/packages/storage-azure/src/utils/getStorageClient.ts @@ -0,0 +1,19 @@ +import type { ContainerClient } from '@azure/storage-blob' + +import { BlobServiceClient } from '@azure/storage-blob' + +import type { AzureStorageOptions } from '../index.js' + +let storageClient: ContainerClient | null = null + +export function getStorageClient( + options: Pick, +): ContainerClient { + if (storageClient) return storageClient + + const { connectionString, containerName } = options + + const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString) + storageClient = blobServiceClient.getContainerClient(containerName) + return storageClient +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0c5d6ffb23..cf89a99337 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1690,6 +1690,9 @@ importers: execa: specifier: 5.1.1 version: 5.1.1 + file-type: + specifier: 17.1.6 + version: 17.1.6 http-status: specifier: 1.6.2 version: 1.6.2 diff --git a/test/package.json b/test/package.json index 6544d84c8c..12258b1d2c 100644 --- a/test/package.json +++ b/test/package.json @@ -53,6 +53,7 @@ "dotenv": "16.4.5", "eslint-plugin-playwright": "1.6.2", "execa": "5.1.1", + "file-type": "17.1.6", "http-status": "1.6.2", "jwt-decode": "4.0.0", "lexical": "0.15.0",