Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2306

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T04:10:09+00:00 2024-11-26T04:10:09+00:00

Uploading files in Remix to a S3 compatible service

  • 61k

Remix is a new react-based framework designed to be a full stack application. With many applications, you will need to be able to store files, sometimes in an S3 compatible service. Here is how I was able to accomplish this. I took heavy influence from this dev.to article.

Create a file named uploader-handler.server.ts with the following contents:

import { s3Client } from './s3.server'; import type { UploadHandler } from '@remix-run/node'; import type { PutObjectCommandInput } from '@aws-sdk/client-s3'; import { GetObjectCommand } from '@aws-sdk/client-s3'; import { PutObjectCommand } from '@aws-sdk/client-s3'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner';  const uploadStreamToS3 = async (data: AsyncIterable<Uint8Array>, key: string, contentType: string) => {   const BUCKET_NAME = "my_bucket_name";    const params: PutObjectCommandInput = {     Bucket: BUCKET_NAME,     Key: key,     Body: await convertToBuffer(data),     ContentType: contentType,   };    await s3Client.send(new PutObjectCommand(params));    let url = await getSignedUrl(s3Client, new GetObjectCommand({     Bucket: BUCKET_NAME,     Key: key,   }), { expiresIn: 15 * 60 });    console.log(url);    return key; }  // The UploadHandler gives us an AsyncIterable<Uint8Array>, so we need to convert that to something the aws-sdk can use.  // Here, we are going to convert that to a buffer to be consumed by the aws-sdk. async function convertToBuffer(a: AsyncIterable<Uint8Array>) {   const result = [];   for await (const chunk of a) {     result.push(chunk);   }   return Buffer.concat(result); }  export const s3UploaderHandler: UploadHandler = async ({ filename, data, contentType }) => {   return await uploadStreamToS3(data, filename!, contentType); } 
Enter fullscreen mode Exit fullscreen mode

Next, you will need to create the actual route to be able to upload a file. I have the following file: ~/routes/api/storage/upload.tsx with the following contents

import type { ActionFunction } from "@remix-run/node"; import { unstable_parseMultipartFormData } from "@remix-run/node"; import { auth } from "~/server/auth.server"; import { s3UploaderHandler } from "~/server/uploader-handler.server";  export const action: ActionFunction = async ({ request }) => {   await auth.isAuthenticated(request, { failureRedirect: '/login' });    const formData = await unstable_parseMultipartFormData(request, s3UploaderHandler);    const fileName = formData.get('upload');    return {     filename: fileName,   } } 
Enter fullscreen mode Exit fullscreen mode

Now that you have the supporting files in place, let's upload a file.

<Form method="post" action={'/api/storage/upload'} encType="multipart/form-data">     <Input type="file" name="upload" />     <Button type="submit">Upload</Button> </Form> 
Enter fullscreen mode Exit fullscreen mode

There you have it!

Version of sdks used:

  • @remix-run/node: 1.6.5
  • @remix-run/react: 1.6.5
  • @aws-sdk/client-s3: 3.145.0
  • @aws-sdk/s3-request-presigner: 3.145.0

reactremixwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.