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,
})
```
This commit is contained in:
Elliot DeNolf
2024-07-09 10:26:13 -04:00
committed by GitHub
parent b4bc7dae11
commit f46ea01df0
4 changed files with 31 additions and 12 deletions

View File

@@ -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 }

View File

@@ -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<AzureStorageOptions, 'connectionString' | 'containerName'>,
): ContainerClient {
if (storageClient) return storageClient
const { connectionString, containerName } = options
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
storageClient = blobServiceClient.getContainerClient(containerName)
return storageClient
}

3
pnpm-lock.yaml generated
View File

@@ -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

View File

@@ -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",