Separate Upload Folders Per Tenant In Multi-Tenant Setup #11967
Replies: 2 comments
-
|
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.
-
|
I'm running into an issue when using the GCS storage plugin with Current BehaviorWhen uploading files through the admin portal, files are uploaded twice to different locations:
Configuration// payload.config.ts
gcsStorage({
clientUploads: true,
collections: {
'my-collection': {
prefix: 'my-collection', // Static prefix
},
},
})
// Collection config
hooks: {
beforeOperation: [
({ args, operation }) => {
if (operation === 'create') {
return {
...args,
data: {
...args.data,
prefix: `my-collection/${args.data.folder}`, // Dynamic prefix
},
}
}
},
],
}What I'm SeeingThe duplicate upload appears to happen because:
Potential ApproachesI'm not entirely sure what the right solution is here, but a few ideas:
I'm curious if there's a recommended pattern for this use case, or if client uploads simply aren't compatible with dynamic prefixes. Current WorkaroundFor now, I can work around this in my custom app by encoding the folder path directly in the filename when generating the signed URL ( |
Beta Was this translation helpful? Give feedback.
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