Separate Upload Folders Per Tenant In Multi-Tenant Setup #11967
bruceparish
started this conversation in
Feature Requests & Ideas
Replies: 1 comment
-
Ran into this today as well and I am also using the AWS S3 bucket adapter. After reviewing the adapter plugin code I came to the following solution: import { getTenantFromCookie } from '@payloadcms/plugin-multi-tenant/utilities'
// My upload-enabled collection config:
const Assets = {
...
fields: [
...
{
name: 'prefix',
type: 'text',
admin: {
hidden: true,
readOnly: true
}
}
],
hooks: {
beforeOperation: [
({ req, operation, args }) => {
if ((operation === 'create' || operation === 'update') && req.file) {
const tenantId =
args.data.tenant ||
getTenantFromCookie(req.headers, 'text') ||
(typeof req.user?.tenant === 'string' ? req.user?.tenant : req.user?.tenant?.id)
if (!tenantId) {
throw new Error('Tenant ID not found when attempting to upload a file')
}
args.data.prefix = tenantId
return args
}
},
],
}
} Relevant parts of the code:
Could perhaps make it work without the custom prefix field by defining a |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
From what I've been able to do currently, I can setup multi-tenancy using the plugin and I can also setup uploads with a prefix. However, that prefix appears to only be a static string and can't be dynamic based on who or what tenant is doing the uploading.
I was able to adjust the filename using the custom filename hook outlined in the documentation, but that only allows you to dynamically set the filename but not a folder. Allowing to setup a folder in a hook or allowing the prefix to accept a function would allow me to setup in my storage provider a separate folder per tenant for better organization and could allow some interesting CDN use cases as well as being able to more easily audit high traffic assets by tenant.
My current solution is to adjust the filename in the hook mentioned above and prefix the filename with the uuid of the tenant, but ideally id have a folder with that uuid rather than part of the filename. Specifically, I've been using S3 which I realize doesn't have real folders, but does allow for virtual foldering and any attempt at setting a folder as a part of the filename gets sanitized.
Thanks for consideration.
Beta Was this translation helpful? Give feedback.
All reactions