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();
})
I need to create PDF file using cloud functions and upload it to firebase storage. I've create pdfBase64string - and just need to save this string-pdf to storage, but can't find information to do it. I tried many different ways but got stuck, because Google answers come to the end.
Notice that you are using the Cloud Storage and not the Firebase Storage library. You can upload with:
// 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!');
});
from the documentation
Now,
The Cloud Storage library is not the same as the Firebase Storage library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.