lib/push-to-s3.js
function upload ({ body, s3bucket, cloudfront, outfile }) {
  const bucketUrl = `https://s3.amazonaws.com/${s3bucket}`
  return new Promise((resolve, reject) => {
    const s3object = new AWS.S3({
      params: {
        ACL: 'public-read',
        Body: body,
        Bucket: s3bucket,
        ContentType: mime.getType(outfile),
        Key: outfile
      }
    })

    const bytes = bytesToSize(body.byteLength || body.length)
    const bucketLink = `<${bucketUrl}/${outfile}|${s3bucket}/${outfile}>`
    s3object.upload().send(function (err) {
      if (err) {
        return reject(
          new Error(
            `s3 upload to ${bucketLink} rejected with ${err.code} ${
              err.message
            }`
          )
        )
      }
      if (cloudfront) {
        const cf = new AWS.CloudFront()
        logger
          .log(`:lightning: *cloudfront:* invalidating path ${outfile}`)
          .then(() => {
            cf.createInvalidation(
              {
                DistributionId: cloudfront,
                InvalidationBatch: {
                  CallerReference: uuid.v4(),
                  Paths: {
                    Items: ['/' + outfile],
                    Quantity: 1
                  }
                }
              },
              function (err) {
                if (err) {
                  return reject(
                    new Error(`cf invalidation rejected with ${err.message}`)
                  )
                }
                done()
              }
            )
          })
      } else {
        done()
      }
    })

    /**
     * Helper function to log a successful upload to s3.
     */
    function done () {
      logger
        .log(`:checkered_flag: *uploaded:* ${bucketLink} (${bytes})`)
        .then(resolve)
    }
  })
}
function upload ({ body, s3bucket, cloudfront, outfile }) {
  const bucketUrl = `https://s3.amazonaws.com/${s3bucket}`
  return new Promise((resolve, reject) => {
    const s3object = new AWS.S3({
      params: {
        ACL: 'public-read',
        Body: body,
        Bucket: s3bucket,
        ContentType: mime.getType(outfile),
        Key: outfile
      }
    })

    const bytes = bytesToSize(body.byteLength || body.length)
    const bucketLink = `<${bucketUrl}/${outfile}|${s3bucket}/${outfile}>`
    s3object.upload().send(function (err) {
      if (err) {
        return reject(
          new Error(
            `s3 upload to ${bucketLink} rejected with ${err.code} ${
              err.message
            }`
          )
        )
      }
      if (cloudfront) {
        const cf = new AWS.CloudFront()
        logger
          .log(`:lightning: *cloudfront:* invalidating path ${outfile}`)
          .then(() => {
            cf.createInvalidation(
              {
                DistributionId: cloudfront,
                InvalidationBatch: {
                  CallerReference: uuid.v4(),
                  Paths: {
                    Items: ['/' + outfile],
                    Quantity: 1
                  }
                }
              },
              function (err) {
                if (err) {
                  return reject(
                    new Error(`cf invalidation rejected with ${err.message}`)
                  )
                }
                done()
              }
            )
          })
      } else {
        done()
      }
    })

    /**
     * Helper function to log a successful upload to s3.
     */
    function done () {
      logger
        .log(`:checkered_flag: *uploaded:* ${bucketLink} (${bytes})`)
        .then(resolve)
    }
  })
}
function upload ( { body , s3bucket , cloudfront , outfile } ) { const bucketUrl = ` ${ s3bucket } ` return new Promise ( ( resolve , reject ) => { const s3object = new AWS . S3 ( { params : { ACL : 'public-read' , Body : body , Bucket : s3bucket , ContentType : mime . getType ( outfile ) , Key : outfile } } ) const bytes = bytesToSize ( body . byteLength || body . length ) const bucketLink = ` ${ bucketUrl } ${ outfile } ${ s3bucket } ${ outfile } ` s3object . upload ( ) . send ( function ( err ) { if ( err ) { return reject ( new Error ( ` ${ bucketLink } ${ err . code } ${ err . message } ` ) ) } if ( cloudfront ) { const cf = new AWS . CloudFront ( ) logger . log ( ` ${ outfile } ` ) . then ( ( ) => { cf . createInvalidation ( { DistributionId : cloudfront , InvalidationBatch : { CallerReference : uuid . v4 ( ) , Paths : { Items : [ '/' + outfile ] , Quantity : 1 } } } , function ( err ) { if ( err ) { return reject ( new Error ( ` ${ err . message } ` ) ) } done ( ) } ) } ) } else { done ( ) } } ) /**
     * Helper function to log a successful upload to s3.
     */ function done ( ) { logger . log ( ` ${ bucketLink } ${ bytes } ` ) . then ( resolve ) } } ) }
Upload the contents of a file to s3.
Also, invalidate the respective cloudfront path if instructed to do so.
Upload the contents of a file to s3 . Also invalidate the respective cloudfront path if instructed to do so .