Last active
April 14, 2022 09:25
-
-
Save liampmccabe/19a78455eb5edec6c451ab428314fd77 to your computer and use it in GitHub Desktop.
remix-uploads.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // New.tsx | |
| export const action: ActionFunction = async ({ request }) => { | |
| const uploadHandler: UploadHandler = async ({ name, filename, stream }) => { | |
| if (name !== "image") { | |
| stream.resume(); | |
| return; | |
| } | |
| try { | |
| const uploadedImage = await uploadImage(stream, filename); | |
| return uploadedImage; | |
| } catch (err) { | |
| console.log(err) | |
| } | |
| }; | |
| const formData = await unstable_parseMultipartFormData( | |
| request, | |
| uploadHandler | |
| ); | |
| } | |
| // Utils.ts | |
| export async function uploadImage( | |
| stream: Readable, | |
| filename: string | |
| ): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| function uploadToS3() { | |
| const pass = new PassThrough(); | |
| const uploadParams = { | |
| Bucket: "bucket", | |
| Key: `${cuid()}-${filename}`, | |
| Body: pass, | |
| }; | |
| console.log(uploadParams); | |
| s3.upload(uploadParams, (err: Error, data: ManagedUpload.SendData) => { | |
| if (err) { | |
| console.log(err); | |
| reject(err); | |
| } | |
| if (data) { | |
| console.log(data); | |
| resolve(data.Location); | |
| } | |
| }); | |
| return pass; | |
| } | |
| stream.pipe(uploadToS3()); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment