const { Storage } = require("@google-cloud/storage");
const storage = new Storage({
keyFilename: "./xxx-path-to-key(locally).json",
projectId: "my project ID",
});
exports.get_data = functions.https.onRequest(async (req, res) => {
const pdf = async () => {
const doc = new PDFDocument();
doc.text("Hello, World!");
doc.end();
return await getStream.buffer(doc);
};
const pdfBuffer = await pdf();
const pdfBase64string = pdfBuffer.toString("base64"); //getting string which I want to send to storage like pdf file
const bucketName = "gs://path-to-storage.com/"
let filename = "test_file.pdf" //some locally pdf file (but can't save locally while using cloud functions)
const uploadFile = async () => {
// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filename, { //this one fine works for locally uploading
metadata: {
cacheControl: "public, max-age=31536000",
contentType: "application/pdf",
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
};
uploadFile();
})我需要使用云函数创建PDF文件,并将其上传到firebase存储。我已经创建了pdfBase64string -只需要将这个字符串-pdf保存到存储中,但是找不到可以这样做的信息。我尝试了许多不同的方法,但都卡住了,因为Google answers最终会结束。
发布于 2020-11-25 17:36:15
请注意,您使用的是云存储,而不是Firebase存储库。您可以使用以下命令进行上传:
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.pdf'
var ref = storageRef.child('mountains.pdf');
// Base64 formatted string
var message = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'base64').then(function(snapshot) {
console.log('Uploaded a base64 string!');
});现在,
云存储库与Firebase存储库不同。
https://stackoverflow.com/questions/64996893
复制相似问题