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

feat: allow for server uploads #993

Merged
merged 2 commits into from
Feb 25, 2025
Merged

feat: allow for server uploads #993

merged 2 commits into from
Feb 25, 2025

Conversation

tefkah
Copy link
Member

@tefkah tefkah commented Feb 25, 2025

Issue(s) Resolved

Not being able to upload files from the server

High-level Explanation of PR

While we have been able to upload files from the frontend for some time, uploading them from the server was not possible, because we were not exposing the S3 client we use for the frontend anywhere else.

This PR exposes that client, and more importantly exposes a convenience function uploadFileToS3 which uses @aws-sdk/lib-storage for easy multipart file uploads, making file uploads much quicker.

Test Plan

  1. Convince yourself the added test makes sense
  2. Make sure frontend uploads still work.

Screenshots (if applicable)

Notes

Comment on lines +32 to +48
describe("assets upload", () => {
it("should be able to upload a file to the minio bucket from the server", async () => {
const { uploadFileToS3 } = await import("./assets");

const file = await fs.readFile(new URL("./assets.ts", import.meta.url));

const url = await uploadFileToS3("test", "test.ts", file, {
contentType: "text/plain",
});

expect(url).toBeDefined();

const text = await fetch(url).then((res) => res.text());

expect(text).toEqual(file.toString("utf8"));
});
});
Copy link
Member Author

Choose a reason for hiding this comment

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

do we need more tests of eg larger files?

Comment on lines +74 to +98
const parallelUploads3 = new Upload({
client,
params: {
Bucket: bucket,
Key: key,
Body: fileData,
ContentType: contentType,
},
queueSize: queueSize ?? 3, // optional concurrency configuration
partSize: partSize ?? 1024 * 1024 * 5, // optional size of each part, in bytes, at least 5MB
leavePartsOnError: false, // optional manually handle dropped parts
});

parallelUploads3.on(
"httpUploadProgress",
progressCallback ??
((progress) => {
logger.debug(progress);
})
);

const result = await parallelUploads3.done();

return result.Location!;
};
Copy link
Member Author

Choose a reason for hiding this comment

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

basically the same as our legacy implementation

https://github.com/pubpub/pubpub/blob/2dfe92a70d647278e512a91dfb060e4480739df2/server/utils/s3.ts#L93-L121

minus the ACL. I'm not sure what it is and why it would be necessary. we always seem to set it to public-read in legacy, but do make uploads with it? eg here
https://github.com/pubpub/pubpub/blob/2dfe92a70d647278e512a91dfb060e4480739df2/server/utils/s3.ts#L93-L121

upload seemed to work fine without it

Copy link
Contributor

@allisonking allisonking left a comment

Choose a reason for hiding this comment

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

hmm the form file upload isn't working for me (the url localhost:9000/assets.v7.pubpub... looks wrong?)

image

will hop over to main now to see if it's happening for me there too!

@allisonking
Copy link
Contributor

nvm it's happening for me on main too.... is it only me?

@tefkah
Copy link
Member Author

tefkah commented Feb 25, 2025

@allisonking it seems to work for me! do you have any overrides in .env.local for any ASSETS_ env vars?
Can you reach the minio server at localhost:9001?

@allisonking
Copy link
Contributor

@allisonking it seems to work for me! do you have any overrides in .env.local for any ASSETS_ env vars? Can you reach the minio server at localhost:9001?

ok don't hold this PR up for me then! I don't think I have any overrides in .env.local. and no I def don't have minio running, which I guess is the problem?—doing a pnpm run dev:setup now!

@tefkah tefkah merged commit 07ab053 into main Feb 25, 2025
6 checks passed
@tefkah tefkah deleted the tfk/upload-server branch February 25, 2025 17:39
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.

3 participants