@@ -60,10 +60,11 @@ function resolveS3ForDatabase(
6060 options : PresignedUrlPluginOptions ,
6161 storageConfig : StorageModuleConfig ,
6262 databaseId : string ,
63+ bucketKey : string ,
6364) : S3Config {
6465 const globalS3 = resolveS3 ( options ) ;
6566 const bucket = options . resolveBucketName
66- ? options . resolveBucketName ( databaseId )
67+ ? options . resolveBucketName ( databaseId , bucketKey )
6768 : globalS3 . bucket ;
6869 const publicUrlPrefix = storageConfig . publicUrlPrefix ?? globalS3 . publicUrlPrefix ;
6970
@@ -127,6 +128,7 @@ export function createDownloadUrlPlugin(
127128 const $key = $parent . get ( 'key' ) ;
128129 const $isPublic = $parent . get ( 'is_public' ) ;
129130 const $filename = $parent . get ( 'filename' ) ;
131+ const $bucketId = $parent . get ( 'bucket_id' ) ;
130132
131133 const $withPgClient = ( grafastContext ( ) as any ) . get ( 'withPgClient' ) ;
132134 const $pgSettings = ( grafastContext ( ) as any ) . get ( 'pgSettings' ) ;
@@ -135,11 +137,12 @@ export function createDownloadUrlPlugin(
135137 key : $key ,
136138 isPublic : $isPublic ,
137139 filename : $filename ,
140+ bucketId : $bucketId ,
138141 withPgClient : $withPgClient ,
139142 pgSettings : $pgSettings ,
140143 } ) ;
141144
142- return lambda ( $combined , async ( { key, isPublic, filename, withPgClient, pgSettings } : any ) => {
145+ return lambda ( $combined , async ( { key, isPublic, filename, bucketId , withPgClient, pgSettings } : any ) => {
143146 if ( ! key ) return null ;
144147
145148 let s3ForDb = resolveS3 ( options ) ;
@@ -155,11 +158,24 @@ export function createDownloadUrlPlugin(
155158 const allConfigs = await loadAllStorageModules ( pgClient , databaseId ) ;
156159 const config = resolveStorageConfigFromCodec ( capturedCodec , allConfigs ) ;
157160 if ( ! config ) return null ;
158- return { config, databaseId } ;
161+
162+ // Look up the bucket key for scoped S3 resolution
163+ let bucketKey = 'public' ;
164+ if ( bucketId ) {
165+ const bucketResult = await pgClient . query ( {
166+ text : `SELECT key FROM ${ config . bucketsQualifiedName } WHERE id = $1 LIMIT 1` ,
167+ values : [ bucketId ] ,
168+ } ) ;
169+ if ( bucketResult . rows [ 0 ] ?. key ) {
170+ bucketKey = bucketResult . rows [ 0 ] . key ;
171+ }
172+ }
173+
174+ return { config, databaseId, bucketKey } ;
159175 } ) ;
160176 if ( resolved ) {
161177 downloadUrlExpirySeconds = resolved . config . downloadUrlExpirySeconds ;
162- s3ForDb = resolveS3ForDatabase ( options , resolved . config , resolved . databaseId ) ;
178+ s3ForDb = resolveS3ForDatabase ( options , resolved . config , resolved . databaseId , resolved . bucketKey ) ;
163179 }
164180 }
165181 } catch {
0 commit comments