Skip to content

Commit

Permalink
feat(storage-azure): expose storage client (#7069)
Browse files Browse the repository at this point in the history
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,
})
```
  • Loading branch information
denolfe committed Jul 9, 2024
1 parent b4bc7da commit f46ea01
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
20 changes: 8 additions & 12 deletions packages/storage-azure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ContainerClient } from '@azure/storage-blob'
import type {
Adapter,
PluginOptions as CloudStoragePluginOptions,
Expand All @@ -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 = {
/**
Expand Down Expand Up @@ -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',
Expand All @@ -133,3 +127,5 @@ function azureStorageInternal({
}
}
}

export { getStorageClientFunc as getStorageClient }
19 changes: 19 additions & 0 deletions packages/storage-azure/src/utils/getStorageClient.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f46ea01

Please sign in to comment.