Documenso 和 aws-smage-upload 示例之间的 Spload 功能比较

在本文中,我们将比较 documenso 和 aws s3 图像上传示例之间将文件上传到 aws s3 所涉及的步骤。

我们从 vercel 提供的简单示例开始。

Documenso 和 aws-smage-upload 示例之间的 Spload 功能比较

示例/aws-s3-image-upload

vercel 提供了一个将文件上传到 aws s3 的良好示例。

此示例的自述文件提供了两个选项,您可以使用现有的 s3 存储桶或创建新存储桶。了解这一点有帮助

您正确配置了上传功能。

又到了我们看源码的时间了。我们正在寻找 type=file 的输入元素。在 app/page.tsx 中,您将找到以下代码:

return (    

upload a file to s3

{ const files = e.target.files if (files) { setfile(files[0]) } }} accept="image/png, image/jpeg" />
)}

onchange

onchange 使用 setfile 更新状态,但不执行上传。当您提交此表单时就会进行上传。

onchange={(e) => {  const files = e.target.files  if (files) {    setfile(files[0])  }}}

处理提交

handlesubmit 函数中发生了很多事情。我们需要分析这个handlesubmit函数中的操作列表。我已在此代码片段中编写了注释来解释这些步骤。

const handlesubmit = async (e: react.formevent) => {    e.preventdefault()    if (!file) {      alert('please select a file to upload.')      return    }    setuploading(true)    const response = await fetch(      process.env.next_public_base_url + '/api/upload',      {        method: 'post',        headers: {          'content-type': 'application/json',        },        body: json.stringify({ filename: file.name, contenttype: file.type }),      }    )    if (response.ok) {      const { url, fields } = await response.json()      const formdata = new formdata()      object.entries(fields).foreach(([key, value]) => {        formdata.append(key, value as string)      })      formdata.append('file', file)      const uploadresponse = await fetch(url, {        method: 'post',        body: formdata,      })      if (uploadresponse.ok) {        alert('upload successful!')      } else {        console.error('s3 upload error:', uploadresponse)        alert('upload failed.')      }    } else {      alert('failed to get pre-signed url.')    }    setuploading(false)  }

api/上传

api/upload/route.ts 有以下代码:

import { createpresignedpost } from '@aws-sdk/s3-presigned-post'import { s3client } from '@aws-sdk/client-s3'import { v4 as uuidv4 } from 'uuid'export async function post(request: request) {  const { filename, contenttype } = await request.json()  try {    const client = new s3client({ region: process.env.aws_region })    const { url, fields } = await createpresignedpost(client, {      bucket: process.env.aws_bucket_name,      key: uuidv4(),      conditions: [        ['content-length-range', 0, 10485760], // up to 10 mb        ['starts-with', '$content-type', contenttype],      ],      fields: {        acl: 'public-read',        'content-type': contenttype,      },      expires: 600, // seconds before the presigned post expires. 3600 by default.    })    return response.json({ url, fields })  } catch (error) {    return response.json({ error: error.message })  }}

handlesubmit 中的第一个请求是 /api/upload 并发送内容类型和文件名作为负载。解析如下:

const { filename, contenttype } = await request.json()

下一步是创建一个 s3 客户端,然后创建一个返回 url 和字段的预签名帖子。您将使用此网址上传您的文件。

有了这些知识,我们来分析一下documenso中的上传工作原理并进行一些比较。

在 documenso 中上传 pdf 文件

让我们从 type=file 的输入元素开始。 documenso 中的代码组织方式不同。您会在名为 document-dropzone.tsx.
的文件中找到输入元素

{_(heading[type])}

这里getinputprops返回的是usedropzone。 documenso 使用react-dropzone。

import { usedropzone } from 'react-dropzone';

ondrop 调用 props.ondrop,你会在 upload-document.tsx 中找到一个名为 onfiledrop 的属性值。


让我们看看 onfiledrop 函数会发生什么。

const onfiledrop = async (file: file) => {    try {      setisloading(true);      const { type, data } = await putpdffile(file);      const { id: documentdataid } = await createdocumentdata({        type,        data,      });      const { id } = await createdocument({        title: file.name,        documentdataid,        teamid: team?.id,      });      void refreshlimits();      toast({        title: _(msg`document uploaded`),        description: _(msg`your document has been uploaded successfully.`),        duration: 5000,      });      analytics.capture('app: document uploaded', {        userid: session?.user.id,        documentid: id,        timestamp: new date().toisostring(),      });      router.push(`${formatdocumentspath(team?.url)}/${id}/edit`);    } catch (err) {      const error = apperror.parseerror(err);      console.error(err);      if (error.code === 'invalid_document_file') {        toast({          title: _(msg`invalid file`),          description: _(msg`you cannot upload encrypted pdfs`),          variant: 'destructive',        });      } else if (err instanceof trpcclienterror) {        toast({          title: _(msg`error`),          description: err.message,          variant: 'destructive',        });      } else {        toast({          title: _(msg`error`),          description: _(msg`an error occurred while uploading your document.`),          variant: 'destructive',        });      }    } finally {      setisloading(false);    }  };

发生了很多事情,但为了我们的分析,我们只考虑名为 putfile 的函数。

putpdf文件

putpdffile 定义在 upload/put-file.ts

/** * uploads a document file to the appropriate storage location and creates * a document data record. */export const putpdffile = async (file: file) => {  const isencrypteddocumentsallowed = await getflag('app_allow_encrypted_documents').catch(    () => false,  );  const pdf = await pdfdocument.load(await file.arraybuffer()).catch((e) => {    console.error(`pdf upload parse error: ${e.message}`);    throw new apperror('invalid_document_file');  });  if (!isencrypteddocumentsallowed && pdf.isencrypted) {    throw new apperror('invalid_document_file');  }  if (!file.name.endswith('.pdf')) {    file.name = `${file.name}.pdf`;  }  removeoptionalcontentgroups(pdf);  const bytes = await pdf.save();  const { type, data } = await putfile(new file([bytes], file.name, { type: 'application/pdf' }));  return await createdocumentdata({ type, data });};

放置文件

这会调用 putfile 函数。

/** * uploads a file to the appropriate storage location. */export const putfile = async (file: file) => {  const next_public_upload_transport = env('next_public_upload_transport');  return await match(next_public_upload_transport)    .with('s3', async () => putfileins3(file))    .otherwise(async () => putfileindatabase(file));};

putfileins3

const putfileins3 = async (file: file) => {  const { getpresignposturl } = await import('./server-actions');  const { url, key } = await getpresignposturl(file.name, file.type);  const body = await file.arraybuffer();  const reponse = await fetch(url, {    method: 'put',    headers: {      'content-type': 'application/octet-stream',    },    body,  });  if (!reponse.ok) {    throw new error(      `failed to upload file "${file.name}", failed with status code ${reponse.status}`,    );  }  return {    type: documentdatatype.s3_path,    data: key,  };};

getpresignposturl

export const getPresignPostUrl = async (fileName: string, contentType: string) => {  const client = getS3Client();  const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner');  let token: JWT | null = null;  try {    const baseUrl = APP_BASE_URL() ?? 'http://localhost:3000';    token = await getToken({      req: new NextRequest(baseUrl, {        headers: headers(),      }),    });  } catch (err) {    // Non server-component environment  }  // Get the basename and extension for the file  const { name, ext } = path.parse(fileName);  let key = `${alphaid(12)}/${slugify(name)}${ext}`;  if (token) {    key = `${token.id}/${key}`;  }  const putObjectCommand = new PutObjectCommand({    Bucket: process.env.NEXT_PRIVATE_UPLOAD_BUCKET,    Key: key,    ContentType: contentType,  });  const url = await getSignedUrl(client, putObjectCommand, {    expiresIn: ONE_HOUR / ONE_SECOND,  });  return { key, url };};

比较

您在 documenso 中没有看到任何 post 请求。它使用名为 getsignedurl 的函数来获取 url,而

vercel 示例向 api/upload 路由发出 post 请求。

在 vercel 示例中可以轻松找到输入元素,因为这只是一个示例,但可以找到 documenso

使用react-dropzone并且输入元素根据业务上下文定位。

关于我们:

在 thinkthroo,我们研究大型开源项目并提供架构指南。我们开发了使用 tailwind 构建的可重用组件,您可以在您的项目中使用它们。

我们提供 next.js、react 和 node 开发服务。

与我们预约会面讨论您的项目。

Documenso 和 aws-smage-upload 示例之间的 Spload 功能比较

参考资料:

https://github.com/documenso/documenso/blob/main/packages/lib/universal/upload/put-file.ts#l69

https://github.com/vercel/examples/blob/main/solutions/aws-s3-image-upload/readme.md

https://github.com/vercel/examples/tree/main/solutions/aws-s3-image-upload

https://github.com/vercel/examples/blob/main/solutions/aws-s3-image-upload/app/page.tsx#l58c5-l76c12

https://github.com/vercel/examples/blob/main/solutions/aws-s3-image-upload/app/api/upload/route.ts

https://github.com/documenso/documenso/blob/main/packages/ui/primitives/document-dropzone.tsx#l157

https://react-dropzone.js.org/

https://github.com/documenso/documenso/blob/main/apps/web/src/app/(dashboard)/documents/upload-document.tsx#l61

https://github.com/documenso/documenso/blob/main/packages/lib/universal/upload/put-file.ts#l22

以上就是Documenso 和 aws-smage-upload 示例之间的 Spload 功能比较的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1499533.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月19日 21:51:22
下一篇 2025年12月19日 21:51:37

相关推荐

  • 使用 Nextjs Tailwind CSS、Prisma、Open AI 和 Clerk 构建的 AI 旅行规划应用程序

    人工智能旅行规划师 使用 next.js 15、tailwind css、prisma、open ai 和 clerk 构建的 ai 旅行规划应用程序。功能包括用户注册、登录、生成旅行计划、查看所有旅行计划和删除行程。在开发过程中接受贡献。 入门 克隆存储库:git clone https://gi…

    2025年12月19日
    000
  • 感谢您的记忆

    认识我的人都知道我的记忆力绝对是垃圾。任何缺少 monty python 对白和 90 年代另类摇滚乐队曲目列表的内容,我都无法接受。 然而,对我们来说幸运的是,计算机在记住事物方面的能力要强得多。 概念 我们今天讨论的技术称为记忆化。让我们从讨论纯函数开始。纯函数背后的想法是,无论你给它什么输入,…

    2025年12月19日
    000
  • LeetCode 的 JavaScript 时代实际上填补了空白

    大多数编码挑战都会教你解决难题。 leetcode 的 30 天 javascript 学习计划做了一些不同的事情:它向您展示了拼图如何变成砖块,准备好构建现实世界的项目。 这种区别很重要。当您解决典型的算法问题时,您正在训练您的思维进行抽象思考。但是,当您实现去抖1函数或构建事件发射器2时,您正在…

    2025年12月19日
    000
  • 在 React JS 项目中设置 Tailwind CSS

    如果您还没有 react 应用程序,请创建一个: npx create-react-app my-appcd my-app 安装 tailwind css运行以下命令安装 tailwind css 及其依赖项: npm install -d tailwindcss postcss autoprefi…

    2025年12月19日
    000
  • Osita 是一位熟练的开发人员,擅长 JavaScript,在 React、Node、Express 方面拥有专业知识

    欢迎来到我的世界 大家好,我是 Christopher Osita,一位充满激情的全栈开发人员、企业家,也是 Siitecch(发音为“see-tech”)背后的创造力。我的职业生涯致力于让技术变得简单、易于使用且有意义。无论您来这里是为了更多地了解我的旅程、我的专业知识,还是我对未来的愿景,欢迎光…

    2025年12月19日
    000
  • JavaScript 中 return 和 return wait 的区别

    您可能会认为这两种方法是相同的。但 return 和 return wait 之间有一个至关重要的区别。 当我们处理promise时,比如数据库查询,我们通常使用await。例如: async function getuserbyid(userid) { const user = await use…

    2025年12月19日
    000
  • 将循环转换为递归:模板和尾递归解释

    递归和循环都是在编程中实现重复任务的基本工具。虽然 for 和 while 等循环对于大多数开发人员来说都很直观,但递归提供了一种更抽象、更灵活的解决问题的方法。本文探讨了如何将循环转换为递归函数,提供通用模板,并解释尾递归的概念和优化。 理解递归 什么是递归? 递归是一种函数调用自身来解决同一问题…

    2025年12月19日
    000
  • 掌握 JavaScript 中的高阶函数

    现代 javascript 开发严重依赖函数式编程,掌握其基本思想将极大提高你的编码能力。 高阶函数是这个范式最有力的武器之一。为了帮助您掌握它们,本文将介绍它们的定义、应用程序和独特的实现。 1. 函数式编程 函数式编程是一种编程范式,强调: 纯函数:没有副作用的函数,对于相同的输入返回相同的输出…

    2025年12月19日
    000
  • Rino,使用 HTML、CSS 和 Typescript/Javascript 的简单静态网站构建器

    快速学习、预处理、直观的网站构建器 rino.js 是您的首选 web 框架,用于使用 html、css 和 typescript/javascript 构建高效的静态网站。它专为各个级别的开发人员而设计,通过将标准 web 技术的强大功能与简化的预处理工具相结合,简化了 web 开发。 要求 no…

    2025年12月19日
    000
  • 了解 JavaScript 中的 async 和 wait:简洁异步代码的关键

    javascript 的异步特性是其最大的优势之一,但它也可能成为开发人员沮丧的根源。随着时间的推移,我们已经从回调函数(以及可怕的“回调地狱”)转向承诺,现在转向异步和等待。这些现代工具简化了异步编程,使您的代码更具可读性、可维护性和高效性。 但是 async 和 wait 到底如何工作,为什么它…

    2025年12月19日 好文分享
    000
  • 为浏览器构建了一个 cli

    泰尔莫 termo 是一个简单的终端模拟器,可用于在您的网站上创建类似终端的界面。它的灵感来自 stripe.dev 中的终端模拟器。它是 xterm.js 之上的包装器。 演示 查看 演示和文档。 github 特征 [x] 可自定义的终端标题、提示、字体和主题[x] 为终端设置欢迎消息[x] 添…

    2025年12月19日
    000
  • 来自 Lamao LiveAPI:构建超级便捷的 API 文档(下)

    在我之前的文章中,我分享了一个由兼职学生组成的小团队如何构建 lama2——一个简化 api 收集和执行的工具。 它很快成为我们工作流程的重要组成部分,但随着我们 API 存储库的增长,Lama2 的手动流程开始显示出其局限性。 扩大 Lama2 规模的挑战 刚开始时,我们的团队由五名学生组成,他们…

    2025年12月19日
    000
  • 上下文、Redux 还是组合?

    这篇文章最初发布于2023年2月23日@我的博客页面 我是受到最近科技公司裁员影响的开发人员之一。所以,我开始用 react 面试前端职位。 在其中一家公司,我在反应中遇到了一个经典的道具钻孔问题,并被要求解决它。为了简单起见,给出的问题就像这个: export default function a…

    2025年12月19日
    000
  • 我的新书:面向 Web 开发人员的全栈 AI – 包含折扣代码!阿玛!

    很高兴分享我的新书《web 开发人员的全栈人工智能》,现已通过 manning 的早期访问计划提供。 经过几个月的工作,我整理了一份实用指南,弥合了传统 Web 开发和人工智能集成之间的差距。本书介绍了使用 React、Next.js、Vercel 和 LangChain 将 AI 融入 Web 应…

    2025年12月19日
    000
  • React 测试:综合指南

    React 是一个流行的 JavaScript 库,用于构建动态且高效的用户界面。为了确保这些应用程序正常运行并随着时间的推移保持可维护性,测试是必不可少的做法。本指南将探讨 React 测试的重要性、其各种类型、工具和最佳实践,帮助您创建可靠且健壮的 React 应用程序。 为什么测试对于 Rea…

    2025年12月19日
    000
  • Hobby API 收集和执行工具如何演变成产品

    在任何初创公司中,跨多个服务管理 api 是一个常见的挑战。 我们面临三个主要问题: 记录 api发布文档每当 api 发生变化时进行更新 每一个都有自己的一系列问题:如何做、在哪里做、使用什么工具以及谁将拥有所有权。 为了解决这个问题,我们的团队决定将所有 api 合并到一个名为 apihub 的…

    2025年12月19日 好文分享
    000
  • 掌握 JavaScript:释放现代 Web 开发的力量

    javascript 从一种简单的网页动画脚本语言到成为现代 web 开发的支柱,已经走过了漫长的道路。无论您是经验丰富的开发人员还是新手,了解 javascript 的功能都可以将您的项目提升到新的高度。在这篇博文中,我们将探讨基本概念和技巧,以帮助您利用 javascript 的真正力量。 1。…

    2025年12月19日
    000
  • Async/Await 与 Promises:JavaScript 初学者简单指南

    您是否有过这样的感觉:您在咖啡店排队等候 javascript 来取拿铁咖啡?异步编程常常给人这样的感觉——同时处理多个订单可能会让您陷入等待。幸运的是,promises 和 async/await 等工具可确保流程保持平稳高效,让您的代码继续运行而不会出现延迟。 在本指南中,我们将详细介绍 pro…

    2025年12月19日
    000
  • 如何使用 JavaScript 在 Bluesky 上发布带有嵌入卡的链接

    随着 bluesky 的不断流行,更多的工具正在围绕它开发。最流行的应用程序之一是后期调度和自动化。 但是,bluesky 的 api 目前不提供直接发布 opengraph 卡片链接的方法。对于想要共享具有有吸引力预览的链接的用户来说,这可能是一个挑战。 在本教程中,我们将向您展示如何使用 jav…

    2025年12月19日
    000
  • AppWorks School – CloudMile 反馈循环项目

    作为训练营计划的一部分,我有机会与 cloudmile 的开发者导师密切合作,开展一个名为“反馈循环”的项目。它是一种活动反馈管理工具,可帮助组织者通过表单提交来跟踪参与者的反馈。我和另一位同学合作完成了这个为期两周的项目,并得到了我们 5 位导师(juri、liang、shan、welly、jac…

    2025年12月19日
    000

发表回复

登录后才能评论
关注微信