【发布时间】:2022-11-05 22:42:50
【问题描述】:
大家好,我正在使用 react-pdf/renderer react pdf 在我的 react 应用程序上渲染 pdf,现在我想将该渲染的 pdf 直接上传到 aws s3 存储我已经成功制作了一个 api 来将文件上传到 s3 存储,并且它在邮递员中工作正常。我可以使用输入类型文件将文件上传到 s3,但我想将渲染的 pdf 直接上传到该存储,以便以后在我的应用程序中检索。共享锅炉 react-pdf 模板我找到了一些解决方案,例如将 react-pdf 转换为缓冲区和流,但我没有得到任何足够的资源或解决方案。
import React from 'react';
import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';
// Create styles
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});
// Create Document Component
const MyDocument = () => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
后端代码
const aws = require('aws-sdk')
const multer = require('multer')
const multerS3 = require('multer-s3');
aws.config.update({
secretAccessKey: process.env.ACCESS_SECRET,
accessKeyId: process.env.ACCESS_KEY,
region: process.env.REGION,
});
const BUCKET = process.env.BUCKET
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
acl: "public-read",
bucket: BUCKET,
key: function (req, file, cb) {
console.log(file);
cb(null, file.originalname)
}
})
})
app.post('/uploadContractTos3', upload.single('file'), async function (req, res, next) {
res.send({s3fileurl : req.file.location})
// res.send("S3 uploaded")
})
【问题讨论】:
标签: reactjs amazon-web-services amazon-s3 react-pdf