Giter VIP home page Giter VIP logo

http-to-s3's Introduction

HTTP to S3

This module streams a HTTP response to an S3 bucket. Both the HTTP response and the S3 upload are implemented using streams allowing large responses to be retrieved without having to worry about memory / disk space limitations.

Installation

npm install --save http-to-s3

Usage

Basic usage

const HttpToS3 = require("http-to-s3");
const httpS3Client = new HttpToS3();

const options = {
  s3: {
    Bucket: "sample-bucket",
    Key: "path/to/file.html"
  }
};
// Defaults to GET request
const uploadResult = await httpS3Client.get("http://example.com", options)

Advanced examples:

Post a body to the URL, stream the results to S3, and log upload progress

const HttpToS3 = require("http-to-s3");
const httpS3Client = new HttpToS3();

const postData = JSON.stringify({ test: true });

const options = {
  request:{
    headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(postData)
    }
  }
  s3: {
    Bucket: "sample-bucket",
    Key: "path/to/file.html"
  },
  upload:{
    uploadProgress: (progress) => {
      console.log(`Uploaded ${progress.loaded} of ${progress.total} for ${progress.key}`)
    }
  }
};

const uploadResult = await httpS3Client.post("http://google.com/upload", options, postData)

Constructor details

new HttpToS3(options = {}) ⇒ Object

The constructor takes an options object with the following parameters:

  • s3 -- < S3 Client > -- an optional instantiated S3 client. See the AWS credentials section for an example.
  • s3Region -- string -- If provided and an S3 client isn't provided, a new client will be created with this as the default region
  • throwFailures -- boolean -- If set to true, the client will throw an error if the server returns a non-2xx response, otherwise an object is returned. Default: false

It returns an object will the following methods:

httpS3Client.request(url, options, body) ⇒ Object

  • url -- string -- a string URL that can be parsed by the Node URL Class

  • options -- object -- an object with two keys, a set of request options and and S3 options

    • request -- an options object that will be fed into the HTTP.request.
      • Note, protocol, host, path, and port will be set automatically from the URL. If method is not set, it will be defaulted to GET
    • s3 -- object -- an options object that will be fed into the S3 upload method
      • Simplest usage is to just provide Bucket and Key
    • upload -- object
      • uploadProgress -- function (progress) -- an optional function that will receive httpUploadProgress events from the S3 ManagedUpload
  • body -- string / Buffer -- An optional string or buffer to be written to the request body

httpS3Client.post(url, options, body) ⇒ Object

The post method is largely identical to the request method, however it will default the method on the request to POST

httpS3Client.get(url, options) ⇒ Object

The post method is largely identical to the request method, however it will default the method on the request to GET

AWS Credentials

This module uses the Node AWS SDK, therefore it will look for credentials in the places specified by the AWS SDK.

If you need to specify your credentials manually, you can provide a pre-configured AWS S3 Client to the constructor.

const AWS = require('aws-sdk');
const HttpToS3 = require('http-to-s3');
const S3Client = new AWS.S3({
  accessKeyId: <key id here>,
  accessKeyId: <secret key here>,
});

const httpS3Client = new HttpToS3({ s3: S3Client });

Tests

Test specs live side-by-side with the file they're testing, following the standard Jest pattern file.test.js.

To run tests: npm test

build

To build the dist files: npm run build

http-to-s3's People

Contributors

chrispasakarnis avatar frsechet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.