Overview
The /api/upload-pdf endpoint provides three methods for managing PDF documents in Amazon S3:
POST : Upload a PDF document
GET : List all PDF documents in a bucket
DELETE : Delete a specific PDF document
POST /api/upload-pdf
Upload a PDF document to an S3 bucket.
Request
The request must be sent as multipart/form-data with the following fields:
The PDF file to upload (must have application/pdf content type)
AWS region where your S3 bucket is located (e.g., us-east-1)
The name of your S3 bucket
AWS Access Key ID for authentication
AWS Secret Access Key for authentication
Optional AWS Session Token for temporary credentials
S3 key prefix for organizing documents (default: documentos)
Response
Success (200 OK)
Always true on successful upload
The S3 bucket where the file was uploaded
The full S3 key of the uploaded file (includes prefix and timestamp)
{
"ok" : true ,
"bucketName" : "my-documents-bucket" ,
"key" : "documentos/1709683200000-my_document.pdf"
}
Uploaded files are stored with the following key format:
{prefix}/{timestamp}-{sanitized-filename}
The filename is sanitized to replace non-alphanumeric characters (except ., _, -) with underscores:
const sanitizeFileName = ( name : string ) : string =>
name . replace ( / [ ^ a-zA-Z0-9._- ] / g , '_' );
const key = ` ${ prefix . replace ( / \/ $ / , '' ) } / ${ Date . now () } - ${ sanitizeFileName ( file . name ) } ` ;
Error Responses
400 Bad Request - Invalid File Type
{
"error" : "El archivo debe ser PDF."
}
400 Bad Request - Missing File
{
"error" : "Debe seleccionar un archivo PDF."
}
400 Bad Request - Missing Parameters
{
"error" : "Faltan parámetros de S3."
}
500 Internal Server Error
{
"error" : "Error message from AWS or internal error"
}
cURL Example
curl -X POST https://your-domain.com/api/upload-pdf \
-F "[email protected] " \
-F "region=us-east-1" \
-F "bucketName=my-documents-bucket" \
-F "accessKeyId=AKIAIOSFODNN7EXAMPLE" \
-F "secretAccessKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
-F "prefix=documentos"
JavaScript Example
const formData = new FormData ();
formData . append ( 'file' , pdfFile );
formData . append ( 'region' , 'us-east-1' );
formData . append ( 'bucketName' , 'my-documents-bucket' );
formData . append ( 'accessKeyId' , 'AKIAIOSFODNN7EXAMPLE' );
formData . append ( 'secretAccessKey' , 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' );
formData . append ( 'prefix' , 'documentos' );
const response = await fetch ( '/api/upload-pdf' , {
method: 'POST' ,
body: formData
});
const data = await response . json ();
console . log ( data . key );
GET /api/upload-pdf
List all PDF documents in an S3 bucket with a specific prefix.
Request Parameters
Query string parameters:
AWS region where your S3 bucket is located
The name of your S3 bucket
AWS Access Key ID for authentication
AWS Secret Access Key for authentication
Optional AWS Session Token for temporary credentials
S3 key prefix to filter documents (default: documentos)
Response
Success (200 OK)
Array of document objects
Full S3 key of the document
ISO 8601 timestamp of last modification, or null
{
"documents" : [
{
"key" : "documentos/1709683200000-report.pdf" ,
"size" : 245678 ,
"lastModified" : "2024-03-05T12:00:00.000Z"
},
{
"key" : "documentos/1709683400000-summary.pdf" ,
"size" : 89012 ,
"lastModified" : "2024-03-05T12:03:20.000Z"
}
]
}
Error Responses
400 Bad Request
{
"error" : "Faltan parámetros de S3 para listar documentos."
}
500 Internal Server Error
{
"error" : "Error message from AWS or internal error"
}
cURL Example
curl -X GET "https://your-domain.com/api/upload-pdf?region=us-east-1&bucketName=my-documents-bucket&accessKeyId=AKIAIOSFODNN7EXAMPLE&secretAccessKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY&prefix=documentos"
JavaScript Example
const params = new URLSearchParams ({
region: 'us-east-1' ,
bucketName: 'my-documents-bucket' ,
accessKeyId: 'AKIAIOSFODNN7EXAMPLE' ,
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' ,
prefix: 'documentos'
});
const response = await fetch ( `/api/upload-pdf? ${ params } ` );
const data = await response . json ();
console . log ( `Found ${ data . documents . length } documents` );
data . documents . forEach ( doc => {
console . log ( ` ${ doc . key } - ${ doc . size } bytes` );
});
DELETE /api/upload-pdf
Delete a specific PDF document from an S3 bucket.
Request Body
The request must be sent as JSON:
AWS region where your S3 bucket is located
The name of your S3 bucket
The full S3 key of the document to delete
AWS Access Key ID for authentication
AWS Secret Access Key for authentication
Optional AWS Session Token for temporary credentials
Response
Success (200 OK)
Always true on successful deletion
The S3 key of the deleted document
{
"ok" : true ,
"key" : "documentos/1709683200000-report.pdf"
}
Error Responses
400 Bad Request
{
"error" : "Faltan parámetros de S3 para eliminar el documento."
}
500 Internal Server Error
{
"error" : "Error message from AWS or internal error"
}
cURL Example
curl -X DELETE https://your-domain.com/api/upload-pdf \
-H "Content-Type: application/json" \
-d '{
"region": "us-east-1",
"bucketName": "my-documents-bucket",
"key": "documentos/1709683200000-report.pdf",
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
}'
JavaScript Example
const response = await fetch ( '/api/upload-pdf' , {
method: 'DELETE' ,
headers: {
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
region: 'us-east-1' ,
bucketName: 'my-documents-bucket' ,
key: 'documentos/1709683200000-report.pdf' ,
accessKeyId: 'AKIAIOSFODNN7EXAMPLE' ,
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
})
});
const data = await response . json ();
console . log ( `Deleted: ${ data . key } ` );
Document Management Learn about the document management interface
S3 Setup Configure your S3 bucket settings