Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions packages/storage-azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,47 @@ export default buildConfig({
})
```

### Authentication methods

Azure Blob Storage supports different authentication methods. Choose the one that best fits your deployment environment.

#### Connection string

Use Azure Storage connection string for straightforward authentication:

```ts
azureStorage({
baseURL: process.env.AZURE_STORAGE_ACCOUNT_BASEURL,
connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
containerName: process.env.AZURE_STORAGE_CONTAINER_NAME,
})
```

#### Azure credentials

Use Azure Identity credentials for enhanced security with managed identities:

```ts
import { DefaultAzureCredential } from '@azure/identity'

azureStorage({
baseURL: process.env.AZURE_STORAGE_ACCOUNT_BASEURL,
credentials: new DefaultAzureCredential(),
containerName: process.env.AZURE_STORAGE_CONTAINER_NAME,
})
```

**Note:** When using User Managed Identity, set the `AZURE_CLIENT_ID` environment variable with your managed identity's client ID.

### Configuration Options

| Option | Description | Default |
| ---------------------- | ------------------------------------------------------------------------ | ------- |
| `enabled` | Whether or not to enable the plugin | `true` |
| `collections` | Collections to apply the Azure Blob adapter to | |
| `allowContainerCreate` | Whether or not to allow the container to be created if it does not exist | `false` |
| `baseURL` | Base URL for the Azure Blob storage account | |
| `connectionString` | Azure Blob storage connection string | |
| `containerName` | Azure Blob storage container name | |
| `clientUploads` | Do uploads directly on the client to bypass limits on Vercel. | |
| Option | Description | Default |
| ---------------------- | -------------------------------------------------------------------------------------------------------- | ------- |
| `enabled` | Whether or not to enable the plugin | `true` |
| `collections` | Collections to apply the Azure Blob adapter to | |
| `allowContainerCreate` | Whether or not to allow the container to be created if it does not exist | `false` |
| `baseURL` | Base URL for the Azure Blob storage account (required when using credentials) | |
| `connectionString` | Azure Blob storage connection string (alternative to credentials + baseURL) | |
| `credentials` | Azure TokenCredential for authentication (e.g., DefaultAzureCredential). Alternative to connectionString | |
| `containerName` | Azure Blob storage container name | |
| `clientUploads` | Do uploads directly on the client to bypass limits on Vercel. | |
1 change: 1 addition & 0 deletions packages/storage-azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"dependencies": {
"@azure/abort-controller": "^1.1.0",
"@azure/core-auth": "^1.9.0",
"@azure/storage-blob": "^12.11.0",
"@payloadcms/plugin-cloud-storage": "workspace:*"
},
Expand Down
15 changes: 13 additions & 2 deletions packages/storage-azure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TokenCredential } from '@azure/core-auth'
import type {
ClientUploadsConfig,
PluginOptions as CloudStoragePluginOptions,
Expand Down Expand Up @@ -55,15 +56,21 @@ export type AzureStorageOptions = {
collections: Partial<Record<UploadCollectionSlug, Omit<CollectionOptions, 'adapter'> | true>>

/**
* Azure Blob storage connection string
* Azure Blob storage connection string (when using connection string authentication)
*/
connectionString: string
connectionString?: string

/**
* Azure Blob storage container name
*/
containerName: string

/**
* Azure Blob storage credentials (alternative to connectionString)
* Use this for UMI (User Managed Identity) or other Azure identity-based authentication
*/
credentials?: TokenCredential

/**
* Whether or not to enable the plugin
*
Expand Down Expand Up @@ -93,8 +100,10 @@ export const azureStorage: AzureStoragePlugin =
(incomingConfig: Config): Config => {
const getStorageClient = () =>
getStorageClientFunc({
baseURL: azureStorageOptions.baseURL,
connectionString: azureStorageOptions.connectionString,
containerName: azureStorageOptions.containerName,
credentials: azureStorageOptions.credentials,
})

const isPluginDisabled = azureStorageOptions.enabled === false
Expand Down Expand Up @@ -123,8 +132,10 @@ export const azureStorage: AzureStoragePlugin =

const createContainerIfNotExists = () => {
void getStorageClientFunc({
baseURL: azureStorageOptions.baseURL,
connectionString: azureStorageOptions.connectionString,
containerName: azureStorageOptions.containerName,
credentials: azureStorageOptions.credentials,
}).createIfNotExists({
access: 'blob',
})
Expand Down
19 changes: 16 additions & 3 deletions packages/storage-azure/src/utils/getStorageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ import type { AzureStorageOptions } from '../index.js'
const azureClients = new Map<string, ContainerClient>()

export function getStorageClient(
options: Pick<AzureStorageOptions, 'clientCacheKey' | 'connectionString' | 'containerName'>,
options: Pick<
AzureStorageOptions,
'baseURL' | 'clientCacheKey' | 'connectionString' | 'containerName' | 'credentials'
>,
): ContainerClient {
const cacheKey = options.clientCacheKey || `azure:${options.containerName}`

if (azureClients.has(cacheKey)) {
return azureClients.get(cacheKey)!
}

const { connectionString, containerName } = options
const { baseURL, connectionString, containerName, credentials } = options

const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
let blobServiceClient: BlobServiceClient

if (credentials) {
blobServiceClient = new BlobServiceClient(baseURL, credentials)
} else if (connectionString) {
blobServiceClient = BlobServiceClient.fromConnectionString(connectionString)
} else {
throw new Error(
'Azure Storage: Either provide a connectionString or credentials for authentication',
)
}

azureClients.set(cacheKey, blobServiceClient.getContainerClient(containerName))

Expand Down
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.

Loading