#How to get request details in utils - s3.server.ts

1 messages · Page 1 of 1 (latest)

opal basalt
graceful olive
#

Add it as a parameter to whatever method you're using

blazing mantle
#

I would create a function that receives the userId and returns the upload handler configured to upload to the bucket of that user

graceful olive
#

The example you're linking to is a bit convoluted since the Bucket parameter on the eventual S3 method being used isn't available to a higher-level api exposed.
I would probably do something like

uploadStream = ({Key, Bucket = STORAGE_BUCKET}): {Key: string, Bucket?: string} => {
  // ...
}

// same thing for uploadStreamToS3, just allow an optional Bucket parameter that will be passed to uploadStream
export async function uploadStreamToS3(data: any, filename: string, Bucket?: string) {/* ... */}

export const s3UploadHandler: UploadHandler = (Bucket?: string) => async({name, fileName, data}) => {
  if (name !== "img") return undefined;
  const uploadedFileLocation = uploadStreamToS3(data, filename!, Bucket);
  return uploadedFileLocation;
}
#

if I was following the same pattern, but basically like sergio said, export a function that returns the upload function bound to a bucket name

opal basalt
#

Thanks Alex. I think im sltill doing it wrong

#

Type '(Bucket?: string | undefined) => ({ name, fileName, data }: { name: any; fileName: any; data: any; }) => Promise<string | undefined>' is not assignable to type 'UploadHandler'.
Types of parameters 'Bucket' and 'part' are incompatible.
Type 'UploadHandlerPart' is not assignable to type 'string'.ts(2322)

#

At the moment im using random folder name

#

Also, is this how i handle file extension / type?

#

where below do I pass userId ```
const uploadHandler: UploadHandler = composeUploadHandlers(
s3UploadHandler,
createMemoryUploadHandler({
maxPartSize: 10000000,
})
);