Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Node Amazon S3 Client.
client.get()
, client.put()
, etc)http.Client
client.putStream()
, client.getFile()
, etc.The following examples demonstrate some capabilities of knox and the S3 REST API. First things first, create an S3 client:
var client = knox.createClient({
key: '<api-key-here>'
, secret: '<secret-here>'
, bucket: 'learnboost'
});
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 Client#put
method with a string or buffer, just like you would for any http.Client
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
request using req.end()
. If we get a 200 response, great!
var object = { foo: "bar" };
var string = JSON.stringify(object);
var req = client.put('/test/obj.json', {
'Content-Length': string.length
, 'Content-Type': 'application/json'
});
req.on('response', function(res){
if (200 == res.statusCode) {
console.log('saved to %s', req.url);
}
});
req.end(string);
By default the x-amz-acl header is private. To alter this simply pass this header to the client request method.
client.put('/test/obj.json', { 'x-amz-acl': 'public-read' });
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
usage:
client.putFile('my.json', '/user.json', function(err, res){
// Logic
});
Another alternative is to stream via Client#putStream()
, for example:
http.get('http://google.com/doodle.png', function(res){
var headers = {
'Content-Length': res.headers['content-length']
, 'Content-Type': res.headers['content-type']
};
client.putStream(res, '/doodle.png', headers, function(err, res){
// Logic
});
});
And if you want a nice interface for putting a buffer or a string of data,
use Client#putBuffer()
:
var buffer = new Buffer('a string of data');
var headers = {
'Content-Type': 'text/plain'
};
client.putBuffer(buffer, '/string.txt', headers, function(err, res){
// Logic
});
Note that both putFile
and putStream
will stream to S3 instead of reading
into memory, which is great. And they return objects that emit 'progress'
events too, so you can monitor how the streaming goes! The progress events have
fields written
, total
, and percent
.
Below is an example GET request on the file we just shoved at S3. It simply outputs the response status code, headers, and body.
client.get('/test/Readme.md').on('response', function(res){
console.log(res.statusCode);
console.log(res.headers);
res.setEncoding('utf8');
res.on('data', function(chunk){
console.log(chunk);
});
}).end();
There is also Client#getFile()
which uses a callback pattern instead of giving
you the raw request:
client.getFile('/test/Readme.md', function(err, res){
// Logic
});
Delete our file:
client.del('/test/Readme.md').on('response', function(res){
console.log(res.statusCode);
console.log(res.headers);
}).end();
Likewise we also have Client#deleteFile()
as a more concise (yet less
flexible) solution:
client.deleteFile('/test/Readme.md', function(err, res){
// Logic
});
As you might expect we have Client#head
and Client#headFile
, following the
same pattern as above.
Knox supports a few advanced operations. Like copying files:
client.copy('/test/Readme.md', '/test/Readme.markdown').on('response', function(res){
console.log(res.statusCode);
console.log(res.headers);
}).end();
// or
client.copyFile('/test/Readme.md', '/test/Readme.markdown', function(err, res){
// ...
});
even between buckets:
client.copyTo('/test/Readme.md', 'mirror-bucket', 'test/Readme.md').on('response', function(res){
// ...
}).end();
// or
client.copyFileTo('/test/Readme.md', 'mirror-bucket', 'test/Readme.md', function (err, res){
// ...
});
or deleting multiple files at once:
client.deleteMultiple(['/test/Readme.md', '/test/Readme.markdown'], function(err, res){
// ...
});
or listing all the files in your bucket:
client.list({ prefix: 'my-prefix' }, function(err, data){
/* `data` will look roughly like:
{
Prefix: 'my-prefix',
IsTruncated: true,
MaxKeys: 1000,
Contents: [
{
Key: 'whatever'
LastModified: new Date(2012, 11, 25, 0, 0, 0),
ETag: 'whatever',
Size: 123,
Owner: 'you',
StorageClass: 'whatever'
},
⋮
]
}
*/
});
And you can always issue ad-hoc requests, e.g. the following to get an object's ACL:
client.request('GET', '/test/Readme.md?acl').on('response', function(res){
// Read and parse the XML response.
// Everyone loves XML parsing.
}).end();
Besides the required key
, secret
, and bucket
options, you can supply any
of the following:
endpoint
By default knox will send all requests to the global endpoint
(bucket.s3.amazonaws.com). This works regardless of the region where the bucket
is. But if you want to manually set the endpoint (for performance reasons) you
can do it with the endpoint
option.
region
For your convenience when using buckets not in the US Standard region, you can
specify the region
option. When you do so, the endpoint
hostname is
automatically assembled.
As of this writing, valid values for the region
option are:
us-standard
us-west-2
us-west-1
eu-west-1
ap-southeast-1
ap-northeast-1
sa-east-1
If new regions are added later, their subdomain names will also work when passed
as the region
option. See the AWS endpoint documentation for
the latest list.
Convenience APIs such as putFile
and 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.
secure
and port
By default, knox uses HTTPS to connect to S3 on port 443. You can override
either of these with the secure
and port
options. Note that if you specify a
custom port
option, the default for secure
switches to false
, although
you can override it manually if you want to run HTTPS against a specific port.
token
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.
agent
If you want to use a custom HTTP agent, you can specify this with the
agent
option.
S3's multipart upload is their rather-complicated way of uploading large files. In particular, it is the only way of streaming files without knowing their Content-Length ahead of time.
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!
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:
{
"key": "<api-key-here>",
"secret": "<secret-here>",
"bucket": "<your-bucket-name>",
"bucketUsWest2": "<bucket-in-us-west-2-region-here>"
}
Then install the dev dependencies and execute the test suite:
$ npm install
$ npm test
FAQs
Amazon S3 client
The npm package knox receives a total of 32,614 weekly downloads. As such, knox popularity was classified as popular.
We found that knox demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.