Node Amazon S3 Client.
- Familiar API (
- Very Node-like low-level request capabilities via
- Higher-level API with
- Copying and multi-file delete support
- Streaming file upload and direct stream-piping support
The following examples demonstrate some capabilities of knox and the S3 REST API. First things first, create an S3 client:
var client = knox;
More options are documented below for features like other endpoints or regions.
If you want to directly upload some strings to S3, you can use the
method with a string or buffer, just like you would for any
request. You pass in the filename as the first parameter, some headers for the
second, and then listen for a
'response' event on the request. Then send the
req.end(). If we get a 200 response, great!
If you send a string, set
Content-Lengthto the length of the buffer of your string, rather than of the string itself.
var object = foo: "bar" ;var string = JSON;var req = client;req;req;
By default the x-amz-acl header is private. To alter this simply pass this header to the client request method.
Each HTTP verb has an alternate method with the "File" suffix, for example
put() also has a higher level method named
putFile(), accepting a source
filename and performing the dirty work shown above for you. Here is an example
Another alternative is to stream via
Client#putStream(), for example:
You can also use your stream's
pipe method to pipe to the PUT request, but
you'll still have to set the
'Content-Length' header. For example:
Finally, if you want a nice interface for putting a buffer or a string of data,
var buffer = 'a string of data';var headers ='Content-Type': 'text/plain';client;
Note that both
putStream will stream to S3 instead of reading
into memory, which is great. And they return objects that emit
events too, so you can monitor how the streaming goes! The progress events have
Below is an example GET request on the file we just shoved at S3. It simply outputs the response status code, headers, and body.
There is also
Client#getFile() which uses a callback pattern instead of giving
you the raw request:
Delete our file:
Likewise we also have
Client#deleteFile() as a more concise (yet less
As you might expect we have
Client#headFile, following the
same pattern as above.
Knox supports a few advanced operations. Like copying files:
even between buckets:
and even between buckets in different regions:
var destOptions = region: 'us-west-2' bucket: 'dest-bucket' ;client;
And you can always issue ad-hoc requests, e.g. the following to get an object's ACL:
Finally, you can construct HTTP or HTTPS URLs for a file like so:
var readmeUrl = client;var userDataUrl = client;
Besides the required
bucket options, you can supply any
of the following:
By default knox will send all requests to the global endpoint
(s3.amazonaws.com). This works regardless of the region where the bucket
is. But if you want to manually set the endpoint, e.g. for performance or
testing reasons, or because you are using a S3-compatible service that isn't
hosted by Amazon, you can do it with the
For your convenience when using buckets not in the US Standard region, you can
region option. When you do so, the
endpoint is automatically
As of this writing, valid values for the
region option are:
- US Standard (default):
- US West (Oregon):
- US West (Northern California):
- EU (Ireland):
- Asia Pacific (Singapore):
- Asia Pacific (Tokyo):
- South America (Sao Paulo):
If new regions are added later, their subdomain names will also work when passed
region option. See the AWS endpoint documentation for
the latest list.
Convenience APIs such as
putStream currently do not work as
expected with buckets in regions other than US Standard without explicitly
specify the region option. This will eventually be addressed by resolving
issue #66; however, for performance reasons, it is always best to specify
the region option anyway.
By default, knox uses HTTPS to connect to S3 on port 443. You can override
either of these with the
port options. Note that if you specify a
port option, the default for
secure switches to
you can override it manually if you want to run HTTPS against a specific port.
If you are using the AWS Security Token Service APIs, you can construct
the client with a
token parameter containing the temporary security
credentials token. This simply sets the x-amz-security-token header on every
request made by the client.
By default, knox tries to use the "virtual hosted style" URLs for accessing S3,
bucket.s3.amazonaws.com. If you pass in
"path" as the
or pass in a
bucket value that cannot be used with virtual hosted style URLs,
knox will use "path style" URLs, e.g.
s3.amazonaws.com/bucket. There are
tradeoffs you should be aware of:
- Virtual hosted style URLs can work with any region, without requiring it to be explicitly specified; path style URLs cannot.
- You can access programmatically-created buckets only by using virtual hosted style URLs; path style URLs will not work.
- You can access buckets with periods in their names over SSL using path style URLs; virtual host style URLs will not work unless you turn off certificate validation.
- You can access buckets with mixed-case names only using path style URLs; virtual host style URLs will not work.
For more information on the differences between these two types of URLs, and limitations related to them, see the following S3 documentation pages:
Knox disables the default HTTP agent, because it leads to lots of "socket
hang up" errors when doing more than 5 requests at once. See #116 for
details. If you want to get the default agent back, you can specify
agent: require("https").globalAgent, or use your own.
Adding the complexity of multipart upload directly to knox is not a great idea. For example, it requires buffering at least 5 MiB of data at a time in memory, which you want to avoid if possible. Fortunately, @nathanoehlman has created the excellent knox-mpu package to let you use multipart upload with knox if you need it!
@superjoe30 has created a nice library, called simply s3, that makes it very easy to upload local files directly to S3, and download them back to your filesystem. For simple cases this is often exactly what you want!
@jergason created intimidate, a library wrapping Knox to automatically retry failed uploads with exponential backoff. This helps your app deal with intermittent connectivity to S3 without bringing it to a ginding halt.
To run the test suite you must first have an S3 account. Then create a file named ./test/auth.json, which contains your credentials as JSON, for example:
Then install the dev dependencies and execute the test suite:
$ npm install$ npm test