@@ -26,14 +26,16 @@ const s3 = new aws.S3({
2626} ) ;
2727
2828const getS3UploadURL = async ( ) => {
29- // generate a unique name for image
30- const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
29+ // generate a unique name for PDF
30+ const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) + ".pdf" ;
3131
32- // set up s3 params
32+ // set up s3 params with proper content type and method
3333 const params = {
3434 Bucket : process . env . S3_BUCKET_NAME ,
3535 Key : fileName ,
3636 Expires : 60 ,
37+ ContentType : 'application/pdf' ,
38+ ACL : 'public-read'
3739 } ;
3840
3941 // get a s3 upload url
@@ -42,28 +44,34 @@ const getS3UploadURL = async () => {
4244 return uploadURL ;
4345} ;
4446
45- const uploadPDF = async ( file : Buffer ) => {
46- const uploadURL = await getS3UploadURL ( ) ;
47+ const uploadPDF = async ( file ) => {
48+ try {
49+ // Generate a unique filename
50+ const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) + ".pdf" ;
51+
52+ console . log ( 'Uploading PDF to S3:' , {
53+ bucket : process . env . S3_BUCKET_NAME ,
54+ key : fileName ,
55+ fileSize : file . buffer . length ,
56+ environment : process . env . NODE_ENV
57+ } ) ;
4758
48- const response = await fetch ( uploadURL , {
49- method : 'PUT' ,
50- body : file . buffer ,
51- headers : {
52- 'Content-Type' : 'application/pdf'
53- }
54- } ) ;
59+ // Upload directly using AWS SDK
60+ const uploadParams = {
61+ Bucket : process . env . S3_BUCKET_NAME ,
62+ Key : fileName ,
63+ Body : file . buffer ,
64+ ContentType : 'application/pdf' ,
65+ ACL : 'public-read'
66+ } ;
5567
56- let fileURL = ""
57- console . error ( 'Response:' , response ) ;
58- if ( response . ok ) {
59- // The URL where your PDF is now accessible (remove the query parameters)
60- fileURL = uploadURL . split ( '?' ) [ 0 ] || "" ;
61- console . log ( 'PDF uploaded successfully to:' , fileURL ) ;
62- } else {
63- console . error ( 'Failed to upload PDF:' , response . statusText ) ;
64- throw new Error ( 'Failed to upload PDF' ) ;
68+ const result = await s3 . upload ( uploadParams ) . promise ( ) ;
69+ console . log ( 'PDF uploaded successfully to:' , result . Location ) ;
70+ return result . Location ;
71+ } catch ( error ) {
72+ console . error ( 'S3 upload error:' , error ) ;
73+ throw new Error ( `Failed to upload PDF: ${ error . message } ` ) ;
6574 }
66- return fileURL ;
6775}
6876
6977export { s3 , uploadPDF } ;
0 commit comments