@@ -44,37 +44,40 @@ const getS3UploadURL = async () => {
4444 return uploadURL ;
4545} ;
4646
47- const uploadPDF = async ( file : Buffer ) => {
47+ const uploadPDF = async ( file ) => {
4848 try {
49- const uploadURL = await getS3UploadURL ( ) ;
50- console . log ( 'Generated upload URL:' , uploadURL ) ;
51-
52- const response = await fetch ( uploadURL , {
53- method : 'PUT' ,
54- body : file . buffer ,
55- headers : {
56- 'Content-Type' : 'application/pdf'
57- }
49+ // Generate a unique filename
50+ const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) + ".pdf" ;
51+
52+ console . log ( 'S3 Configuration:' , {
53+ region : region ,
54+ bucket : process . env . S3_BUCKET_NAME ,
55+ hasAccessKey : ! ! accessKeyId ,
56+ hasSecretKey : ! ! secretAccessKey
5857 } ) ;
5958
60- console . log ( 'S3 Response status:' , response . status ) ;
61- console . log ( 'S3 Response statusText:' , response . statusText ) ;
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+ } ;
67+
68+ console . log ( 'Uploading to S3 with params:' , {
69+ Bucket : process . env . S3_BUCKET_NAME ,
70+ Key : fileName ,
71+ ContentType : 'application/pdf' ,
72+ BodySize : file . buffer . length
73+ } ) ;
6274
63- let fileURL = ""
64- if ( response . ok ) {
65- // The URL where your PDF is now accessible (remove the query parameters)
66- fileURL = uploadURL . split ( '?' ) [ 0 ] || "" ;
67- console . log ( 'PDF uploaded successfully to:' , fileURL ) ;
68- } else {
69- const errorText = await response . text ( ) ;
70- console . error ( 'Failed to upload PDF:' , response . statusText ) ;
71- console . error ( 'Error response body:' , errorText ) ;
72- throw new Error ( `Failed to upload PDF: ${ response . statusText } - ${ errorText } ` ) ;
73- }
74- return fileURL ;
75+ const result = await s3 . upload ( uploadParams ) . promise ( ) ;
76+ console . log ( 'PDF uploaded successfully to:' , result . Location ) ;
77+ return result . Location ;
7578 } catch ( error ) {
76- console . error ( 'Upload error:' , error ) ;
77- throw error ;
79+ console . error ( 'S3 upload error:' , error ) ;
80+ throw new Error ( `Failed to upload PDF: ${ error . message } ` ) ;
7881 }
7982}
8083
0 commit comments