title: Send Binary Image from Serverless AWS Lambda Over FTP
body: <p>I'm building an AWS Lambda API, using the serverless stack, that will receive an image as a binary string then store that image on GitHub and send it over FTP to a static app server.  I am NOT storing the image in S3 at all as I am required not to. </p>
  
  <p>I have already figured out how to save the image to GitHub (by converting binary to base64), the issue is sending the binary image data over FTP to another server to hold statically.  The static image server is pre-existing and I cannot use S3.  I am using the <a href="https://www.npmjs.com/package/ftp" rel="nofollow noreferrer">ftp</a> npm package to send the image.  The image server indeed receives the image BUT NOT in the correct format and the image is just non-displayable junk data.  I saw <a href="https://stackoverflow.com/questions/21501413/read-binary-data-from-an-image-and-save-it-with-javascript">an example</a> on how to do this on the client side by encoding the image in an Uint16Array and passing it to a new Blob() object, but sadly, NODE.JS DOES NOT SUPPORT BLOBS!</p>
  
  <p>Using <a href="https://www.npmjs.com/package/ftp" rel="nofollow noreferrer">ftp</a> npm module:</p>
  
  <pre><code>    async sendImage(image) {
          try {
              const credentials = await getFtpCredentials(this.platform);
              console.log('Sending files via FTP client');
              return new Promise((resolve, reject) =&gt; {
                  let ftpClient = new ftp();
                  ftpClient.on('ready', () =&gt; {
                      console.log(`Putting ${image.path} onto server`);
                      // Set transfer type to binary
                      ftpClient.binary(err =&gt; {
                          if(err)
                              reject(err);
                      });
                      // image.content is binary string data
                      ftpClient.put(image.content, image.path, (err) =&gt; {
                          if (err) {
                              reject(err);
                          }
                          console.log('Closing FTP connection');
                          ftpClient.end();
                          resolve(`FTP PUT for ${image.path} successful!`);
                      });
                  });
                  console.log('Connecting to FTP server');
                  ftpClient.connect(credentials);
              });
          } catch(err) {
              console.log('Failed to load image onto FTP server');
              throw err;
          }
      }
  
  </code></pre>
  
  <p>This procedure sends the binary data to the server, but the data is un-readable by any browser or image viewer.  Do I need to use another FTP package?  Or am I just not encoding this right??  I've spent days googleing the answer to this seemingly common task and it's driving me up the wall!  Can anyone instruct me on how to send binary image data from a node.js lambda function over FTP to another server so that the image is encoded properly and works when viewed?  ANY help is very much appreciated!</p>