APIs & Integrations

TBaxter
Member

500 Internal Server Error when Attempting to Upload File

I'm receiving a 500 error when attempting to upload a file from in an AWS Lambda function from an S3 file object. You can see that I am passing in the `Body` from the s3 object, which is a Buffer.

import { config } from '../../config';                                                                  
import request from 'request';                                                                          
import S3 from 'aws-sdk/clients/s3';                                                                    
import util from 'util';                                                                                
                                                                                                        
export const uploadFileToHubspot = async (                                                              
  email: string,                                                                                        
  filePath: string                                                                                      
): Promise<number | null> => {                                                                          
  const { MY_SOURCE_BUCKET, HUBSPOT_API_KEY } = config;                                   
                                                                                                        
  // Read in the data from the S3 Bucket                                                                
  const s3 = new S3();                                                                                  
  const sourceFile = await s3                                                                           
    .getObject({                                                                                        
      Bucket: MY_SOURCE_BUCKET,                                                           
      Key: filePath,                                                                                    
    })                                                                                                  
    .promise();                                                                                         
                                                                                                        
  const postUrl = `https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=${HUBSPOT_API_KEY}`;  
  const requestPromisePost = util.promisify(request.post);                                              
  const fileOptions = {                                                                                 
    access: 'PRIVATE',                                                                                  
    duplicateValidationScope: 'ENTIRE_PORTAL',                                                          
    duplicateValidationStrategy: 'NONE',                                                                
    overwrite: true,                                                                                    
    ttl: 'P3M',                                                                                         
  };                                                                                                    
                                                                                                        
  const postRequest = {                                                                                 
    formData: {                                                                                         
      file: sourceFile.Body,                                                                            
      folderPath: email,                                                                                
      options: JSON.stringify(fileOptions),                                                             
    },                                                                                                  
    headers: { 'content-type': 'multipart/form-data' },                                                 
    url: postUrl,                                                                                       
  };                                                                                                    
  const response = await requestPromisePost(postRequest);                                               
                                                                                                        
  if (response.statusMessage !== 'OK') return null;                                                     
                                                                                                        
  const responseBody = JSON.parse(response.body);                                                       
                                                                                                        
  return responseBody.objects[0].id;                                                                    
};                                                                                                      
                                                                                                        
                                                                                                        

This is failing with

{"status":"error","message":"internal error","correlationId":"90765b9e-8b9b-45c5-9cef-9771ab9df345"}

 

I'm assuming this is a problem passing in the buffer, if I call `toString()` on the data object I do get the `400` error (which provides precisely zero information as to what the error is.

 

I'm at a loss here, any pointers - specifically with regards to uploading data to hubspot from an S3 file object - would be greatly appreciated.

0 Upvotes
2 Replies 2
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

500 Internal Server Error when Attempting to Upload File

@TBaxter - Are you sure the s3.getObject function is returning a blob and not an object.... can you please debug the output of that response? Maybe this thread might help https://github.com/aws/aws-sdk-js-v3/issues/1877

dennisedson
HubSpot Product Team
HubSpot Product Team

500 Internal Server Error when Attempting to Upload File

@tjoyce , any thoughts on what is happening here?