reactjs - 在反应中使用 Firebase 存储
问题描述
我正在尝试将使用 dataToUrl 创建的 pdf 上传到 firebase,但我不断获得未经授权的 403 存储。
我做了 npm i @firebase/storage --save 并在我的 package.json 中说:
"@firebase/storage": "^0.3.32",
我有一个存储我的 firebase 凭据的文件 firebase.js:
import * as firebase from 'firebase';
import 'firebase/auth';
import 'firebase/firestore';
import '@firebase/storage';
const firebaseConfig = {
...my details here
};
firebase.initializeApp(firebaseConfig);
export default firebase;
如果我尝试像文档中所说的那样初始化存储桶,那么它不会加载:
const bucket = "my-app-xxxxx.appspot.com/";
firebase.initializeApp(
{
credential: firebaseConfig,
storageBucket: bucket
}
);
我将 DOM 转换为 jpeg,然后将 jpeg 转换为 pdf 然后存储 pdf 的代码到目前为止是这样的:
import firebase from '../../Utils/firebase';
import uuid from 'react-uuid'
import 'firebase/auth';
import 'firebase/firestore';
import '@firebase/storage';
domtoimage.toJpeg(node, {width: reportInViewerRef.current.offsetWidth * 2, height: reportInViewerRef.current.scrollHeight * 2})
.then(function (dataUrl) {
const pdf = new jsPDF('p', 'px', [reportHeight - 20, reportWidth]);
pdf.addImage(dataUrl, 'JPEG', 0, -12, reportWidth, reportHeight);
pdf.save("download.pdf");
var storageRef = firebase.storage().ref();
var fileLocation = getUniqueFilename() + '.jpg'
var reportPdfRef = storageRef.child(fileLocation);
reportPdfRef.putString(dataUrl, 'data_url').then(function(snapshot) {
console.log('Uploaded a data_url string!', reportPdfRef);
});
// download(dataUrl, 'my-node.jpg')
})
.catch(function (error) {
console.error('oops, something went wrong!', error);
});
我的存储规则是:
rules_version = '2';
service firebase.storage {
match /b/my-app-xxxxx.appspot.com/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
任何见解表示赞赏!
解决方案
推荐阅读
- java - 如何在 Java 中生成 AES 128 位密钥。我想要这样的东西。Es8NxqG/f3VMzcd9mrPSQQ==
- javascript - Firebase 'child_changed' 听众理解
- http - Kubernetes 阻止混合活动内容
- docker - OSSEC_HIDS Kubernetes 部署
- java - SecureRandom 能否产生可预测的随机序列
- angular - 获取 Angular CLI 版本
- aws-sdk - 使用 SageMaker 对象检测进行预测的图像大小?
- flutter - Flutter 有很多嵌套的小部件是不是很糟糕?
- java-8 - AOSP 构建期间 API 更改错误。怎么修?
- javascript - nodejs 加密模块 privateEncrypt() 总是返回相同的结果