ryanto / next-s3-upload

Upload files from your Next.js app to S3
https://next-s3-upload.codingvalue.com/
570 stars 78 forks source link

Pass `Content-MD5` header to presigned upload URL #140

Open safoine27 opened 1 year ago

safoine27 commented 1 year ago

The application we are working with uploads objects to buckets with Object Lock enabled, where a retention period is set, using usePresignedUpload().

However, as per the AWS documentation, it is currently impossible to upload objects within buckets with Object Lock enabled and a retention period set, unless the Content-MD5 header is included in the request. As explained from the AWS docs

The Content-MD5 header is required for any request to upload an object with a retention period configured using Amazon S3 Object Lock

Currently, s3-upload is only able to pass custom headers to /api/s3-upload, and the only headers that we are passing to the PutObject request are Content-Type and Cache-Control.

To address this issue, we want to pass the MD5 digest to the request inside https://github.com/ryanto/next-s3-upload/blob/master/packages/next-s3-upload/src/hooks/use-presigned-upload.ts#L15-L16. The header should be optional and the digest value should be calculated from the application code.

I’d be happy to open a pull request to enable the passing of the Content-MD5 header (or any other extra headers) to the pre-signed upload request.

williamrjribeiro commented 1 year ago

That's a very handy feature that I would love very much to see added to this library.

But... since the MD5 Digest is required by AWS for immutable buckets, maybe the library could calculate the digest for the application? 💭

I'm not sure if the philosophy of this library is "battery included", but if it is, then I believe it would be a perfect fit. The exposed API could be something simple like a property:

const { uploadToS3 } = usePresignedUpload({ isImmutableBucket: true });

What y'all think?