Skip to content

Commit f46ea01

Browse files
authored
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, }) ```
1 parent b4bc7da commit f46ea01

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

packages/storage-azure/src/index.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ContainerClient } from '@azure/storage-blob'
21
import type {
32
Adapter,
43
PluginOptions as CloudStoragePluginOptions,
@@ -7,13 +6,13 @@ import type {
76
} from '@payloadcms/plugin-cloud-storage/types'
87
import type { Config, Plugin } from 'payload'
98

10-
import { BlobServiceClient } from '@azure/storage-blob'
119
import { cloudStoragePlugin } from '@payloadcms/plugin-cloud-storage'
1210

1311
import { getGenerateURL } from './generateURL.js'
1412
import { getHandleDelete } from './handleDelete.js'
1513
import { getHandleUpload } from './handleUpload.js'
1614
import { getHandler } from './staticHandler.js'
15+
import { getStorageClient as getStorageClientFunc } from './utils/getStorageClient.js'
1716

1817
export type AzureStorageOptions = {
1918
/**
@@ -105,19 +104,14 @@ function azureStorageInternal({
105104
connectionString,
106105
containerName,
107106
}: AzureStorageOptions): Adapter {
108-
let storageClient: ContainerClient | null = null
109-
const getStorageClient = () => {
110-
if (storageClient) return storageClient
111-
112-
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
113-
storageClient = blobServiceClient.getContainerClient(containerName)
114-
return storageClient
115-
}
116-
117107
const createContainerIfNotExists = () => {
118-
void getStorageClient().createIfNotExists({ access: 'blob' })
108+
void getStorageClientFunc({ connectionString, containerName }).createIfNotExists({
109+
access: 'blob',
110+
})
119111
}
120112

113+
const getStorageClient = () => getStorageClientFunc({ connectionString, containerName })
114+
121115
return ({ collection, prefix }): GeneratedAdapter => {
122116
return {
123117
name: 'azure',
@@ -133,3 +127,5 @@ function azureStorageInternal({
133127
}
134128
}
135129
}
130+
131+
export { getStorageClientFunc as getStorageClient }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { ContainerClient } from '@azure/storage-blob'
2+
3+
import { BlobServiceClient } from '@azure/storage-blob'
4+
5+
import type { AzureStorageOptions } from '../index.js'
6+
7+
let storageClient: ContainerClient | null = null
8+
9+
export function getStorageClient(
10+
options: Pick<AzureStorageOptions, 'connectionString' | 'containerName'>,
11+
): ContainerClient {
12+
if (storageClient) return storageClient
13+
14+
const { connectionString, containerName } = options
15+
16+
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
17+
storageClient = blobServiceClient.getContainerClient(containerName)
18+
return storageClient
19+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"dotenv": "16.4.5",
5454
"eslint-plugin-playwright": "1.6.2",
5555
"execa": "5.1.1",
56+
"file-type": "17.1.6",
5657
"http-status": "1.6.2",
5758
"jwt-decode": "4.0.0",
5859
"lexical": "0.15.0",

0 commit comments

Comments
 (0)