Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for additional S3 providers #71

Merged
merged 5 commits into from
Jan 31, 2024
Merged

Conversation

mertd
Copy link
Contributor

@mertd mertd commented Jan 30, 2024

Adding support for additional S3 providers to avoid vendor lock-in and for a better self-hosting experience. See #65

I successfully tested integration with a local instance of Minio. I have not tested these changes with a standard AWS S3 setup.

Comment on lines 8 to 11
accessKeyId: process.env.S3_UPLOAD_KEY,
secretAccessKey: process.env.S3_UPLOAD_SECRET,
bucket: process.env.S3_UPLOAD_BUCKET,
region: process.env.S3_UPLOAD_REGION,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are superfluous i think, only endpoint and forcePathStyle are needed if the rest are the default env vars used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I removed them as they were not needed indeed.

@@ -29,7 +29,7 @@ type Props = {

export function ExpenseDocumentsInput({ documents, updateDocuments }: Props) {
const [pending, setPending] = useState(false)
const { FileInput, openFileDialog, uploadToS3 } = useS3Upload()
const { FileInput, openFileDialog, uploadToS3 } = usePresignedUpload() // use presigned uploads to addtionally support providers other than AWS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this (since I'm no expert on js/ts), but you could continue to use the useS3Upload() function if !!process.env.S3_UPLOAD_ENDPOINT, since it's recommended for various reasons by the next-s3-upload lib.

So import both, and set the variables depending on the env var.

If this doesn't work for whatever client/server magic reasons that always happens with next, you may also ignore this comment :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is on the client side, the environment variable would need to be exposed using the NEXT_PUBLIC_ prefix and defined at build time. This leads to two options:

  1. use useS3Upload by default since it is recommended and add a slightly redundant environment variable (e.g. NEXT_PUBLIC_ENABLE_CUSTOM_S3_ENDPOINT) that indicates whether usePresignedUpload should be used instead.
  2. use usePresignedUpload for any provider since the only documented limitation is that AWS S3 has a file limit of 1GB on presigned uploads

I went with the latter option for a bit less complexity and because I assumed that there is no current use case for uploads larger than 1GB, but I am open to changing the approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I meant about the client/server magic, so then I agree that this is the better solution, but @scastiel has the last word on the PR anyhow 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree to use usePresignedUpload everytime if it doesn’t cause any issue with S3. Plus it’s tricky to use a hook conditionally with React 😉

Copy link
Member

@scastiel scastiel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing 💯

Comment on lines 8 to 10
endpoint: process.env.S3_UPLOAD_ENDPOINT,
// forcing path style is only necessary for providers other than AWS
forcePathStyle: !!process.env.S3_UPLOAD_ENDPOINT,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using env from @/lib/env when on the server, as you get type safe values 😉

  endpoint: env.S3_UPLOAD_ENDPOINT,
  // forcing path style is only necessary for providers other than AWS
  forcePathStyle: !!env.S3_UPLOAD_ENDPOINT,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I am now using the typed environment variables. I also resolved the merge conflicts introduced by the recent changes on main.

@@ -29,7 +29,7 @@ type Props = {

export function ExpenseDocumentsInput({ documents, updateDocuments }: Props) {
const [pending, setPending] = useState(false)
const { FileInput, openFileDialog, uploadToS3 } = useS3Upload()
const { FileInput, openFileDialog, uploadToS3 } = usePresignedUpload() // use presigned uploads to addtionally support providers other than AWS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree to use usePresignedUpload everytime if it doesn’t cause any issue with S3. Plus it’s tricky to use a hook conditionally with React 😉

@justcallmelarry
Copy link
Contributor

justcallmelarry commented Jan 30, 2024

I did try this out towards my own hosted version of Minio (behind a reverse proxy) and it works like a charm 👌
I did not test it out against an actual S3 bucket, though.

EDIT: Also tested that it works when compiled, with the docker image

@scastiel
Copy link
Member

Just tested that everything still works with the S3 bucket 👍

@scastiel scastiel merged commit 08d75fd into spliit-app:main Jan 31, 2024
1 check passed
@mertd mertd deleted the s3-endpoint branch January 31, 2024 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants