From f46ea01df0379a41e2f3ee8714076589fc1eba70 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Tue, 9 Jul 2024 10:26:13 -0400 Subject: [PATCH] feat(storage-azure): expose storage client (#7069) Expose the storage client for re-use. ```ts import { getStorageClient } from '@payloadcms/storage-azure' import { getPayload } from 'payload' const awaitedConfig = await importConfig('./config.ts') const payload = await getPayload({ config: awaitedConfig }) // Get internal azure blob storage client const storageClient = getStorageClient({ connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING, containerName: process.env.AZURE_STORAGE_CONTAINER_NAME, }) ``` --- packages/storage-azure/src/index.ts | 20 ++++++++----------- .../src/utils/getStorageClient.ts | 19 ++++++++++++++++++ pnpm-lock.yaml | 3 +++ test/package.json | 1 + 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 packages/storage-azure/src/utils/getStorageClient.ts 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",