@@ -26,16 +26,14 @@ const s3 = new aws.S3({
2626} ) ;
2727
2828const getS3UploadURL = async ( ) => {
29- // generate a unique name for PDF
30- const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) + ".pdf" ;
29+ // generate a unique name for image
30+ const fileName = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
3131
32- // set up s3 params with proper content type and method
32+ // set up s3 params
3333 const params = {
3434 Bucket : process . env . S3_BUCKET_NAME ,
3535 Key : fileName ,
3636 Expires : 60 ,
37- ContentType : 'application/pdf' ,
38- ACL : 'public-read'
3937 } ;
4038
4139 // get a s3 upload url
@@ -44,34 +42,27 @@ const getS3UploadURL = async () => {
4442 return uploadURL ;
4543} ;
4644
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- } ) ;
45+ const uploadPDF = async ( file : Buffer ) => {
46+ const uploadURL = await getS3UploadURL ( ) ;
5847
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- } ;
48+ const response = await fetch ( uploadURL , {
49+ method : 'PUT' ,
50+ body : file . buffer ,
51+ headers : {
52+ 'Content-Type' : 'application/pdf'
53+ }
54+ } ) ;
6755
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 } ` ) ;
56+ let fileURL = ""
57+ if ( response . ok ) {
58+ // The URL where your PDF is now accessible (remove the query parameters)
59+ fileURL = uploadURL . split ( '?' ) [ 0 ] || "" ;
60+ console . log ( 'PDF uploaded successfully to:' , fileURL ) ;
61+ } else {
62+ console . error ( 'Failed to upload PDF:' , response . statusText ) ;
63+ throw new Error ( 'Failed to upload PDF' ) ;
7464 }
65+ return fileURL ;
7566}
7667
7768export { s3 , uploadPDF } ;
0 commit comments