Storing PDFs in Cloud Storage
Save generated PDFs to AWS S3, Google Cloud Storage, or Azure Blob Storage for long-term archival.
Overview
After generating a PDF, you may want to archive it in your own cloud storage for compliance, backup, or serving directly to users. This guide covers uploading to major cloud providers.
AWS S3
Upload to S3
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { DocumentStack } from "@document-stack/sdk-node";
const ds = new DocumentStack({ apiKey: process.env.DS_API_KEY! });
const s3 = new S3Client({ region: "us-east-1" });
async function generateAndStore(templateId: string, data: any) {
// Generate the PDF
const pdf = await ds.generate({ templateId, data });
// Download the PDF
const response = await fetch(pdf.url);
const buffer = Buffer.from(await response.arrayBuffer());
// Upload to S3
const key = `invoices/${Date.now()}.pdf`;
await s3.send(new PutObjectCommand({
Bucket: "my-documents-bucket",
Key: key,
Body: buffer,
ContentType: "application/pdf",
}));
return `s3://my-documents-bucket/${key}`;
}Google Cloud Storage
Upload to GCS
import { Storage } from "@google-cloud/storage";
import { DocumentStack } from "@document-stack/sdk-node";
const ds = new DocumentStack({ apiKey: process.env.DS_API_KEY! });
const storage = new Storage();
const bucket = storage.bucket("my-documents-bucket");
async function generateAndStore(templateId: string, data: any) {
const pdf = await ds.generate({ templateId, data });
const response = await fetch(pdf.url);
const buffer = Buffer.from(await response.arrayBuffer());
const filename = `invoices/${Date.now()}.pdf`;
const file = bucket.file(filename);
await file.save(buffer, {
contentType: "application/pdf",
metadata: { templateId },
});
return `gs://my-documents-bucket/${filename}`;
}Azure Blob Storage
Upload to Azure Blob
import { BlobServiceClient } from "@azure/storage-blob";
import { DocumentStack } from "@document-stack/sdk-node";
const ds = new DocumentStack({ apiKey: process.env.DS_API_KEY! });
const blobService = BlobServiceClient.fromConnectionString(
process.env.AZURE_STORAGE_CONNECTION!
);
const container = blobService.getContainerClient("documents");
async function generateAndStore(templateId: string, data: any) {
const pdf = await ds.generate({ templateId, data });
const response = await fetch(pdf.url);
const buffer = Buffer.from(await response.arrayBuffer());
const blobName = `invoices/${Date.now()}.pdf`;
const blob = container.getBlockBlobClient(blobName);
await blob.uploadData(buffer, {
blobHTTPHeaders: { blobContentType: "application/pdf" },
});
return blob.url;
}File Organization
Use a consistent naming convention for easy retrieval:
Recommended Structure
documents/
invoices/
2025/01/
inv-001-alice-johnson.pdf
inv-002-bob-smith.pdf
certificates/
2025/01/
cert-042-alice-johnson.pdf
reports/
2025/01/
monthly-report-january.pdfMetadata
Store metadata (template ID, customer ID, generation date) as object tags or metadata in your cloud storage for easy querying and audit trails.Next Steps
- Batch Generation — Bulk storage workflows
- Email Attachments — Send stored PDFs
- Data Retention — Retention policies