npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s3policy-acl

v0.0.1

Published

S3 Upload/Download Policy Generator for Node.js

Downloads

6

Readme

s3policy

S3 Upload/Download Policy Generator for Node.js

This is a simple node.js module for easliy creating signed URLs for downloading resources directly from Amazon S3 as well as creating the necessary data for doing file uploads directly to S3. The great thing about using these methods is that file download/uploaded happen between the client and S3, your applications servers don't have to burdened with the traffic. This is all done while still keeping it highly secure.

This module is available via npm by running npm install s3policy

In order to use this module, you will first need to have an AWS account and an IAM user with the necessary access to the read/write the files you want to create links for.

Here is a sample to create a signed URL to download the file 'myfile' from the bucket 'mybucket' that will be good for the next 60 seconds.

var s3 = require('s3policy');
var myS3Account = new s3('MYACCESSKEY', 'MYSECRETKEY');
var url = myS3Account.readPolicy('myfile', 'mybucket', 60);

If you want to force the browser to initiate a file download add filename to the 4th parameter

var url = myS3Account.readPolicy('myfile', 'mybucket', 60, 'file.txt');

Allowing the client browser to upload to S3 is a bit more complicated. Check out http://aws.amazon.com/articles/1434 for more information. s3policy helps out by creating the necessary signed variables that the client will need to upload. Here is a sample to return an object with the signed variables to allow a 10MB upload to 'myfile' in the 'mybucket' bucket, you would normally have the client do an AJAX request to get this info when it commences an upload.

var s3 = require('s3policy');
var myS3Account = new s3('MYACCESSKEY', 'MYSECRETKEY');
var policy = myS3Account.writePolicy('myfile', 'mybucket', 60, 10, 'public-read', callback);

HTML form

<form id="myForm" action="https://[bucket-name].s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    <input type="hidden" id="key" name="key" value="uploads/${filename}">
    <input type="hidden" id="acl" name="acl" value="YOUR_ACL_OPTION">
    <input type="hidden" id="AWSAccessKeyId" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY"> 
    <input type="hidden" id="policy" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
    <input type="hidden" id="signature" name="signature" value="YOUR_CALCULATED_SIGNATURE">
    <input type="hidden" name="Content-Type" value="MIME_TYPE">
    <input name="file" id="file" type="file"> 
<input id="btn_submit" class="btn btn-warning" type="submit" value="Upload File to S3"> 
</form>

Client-side scripts

function handleRes(res,req,err) {
        $("#AWSAccessKeyId").val(res.s3Key);
        $("#policy").val(res.s3PolicyBase64);
        $("#signature").val(res.s3Signature);
        $("#acl").val(res.acl);
        $("#mime").val(res.mime);
        $("#myForm").submit();
}
$( document ).ready(function() {
        $( "#btn_submit" ).bind( "click", requestCredentials );
        console.log( "ready!" );
});


var requestCredentials = function(event) { 
    event.preventDefault(); // intercept and override the submit button 
    var _file;

    _file = $("#file").val().replace(/.+[\\\/]/, "");

    $.ajax({ // example of a simple ajax request
        url: "http://localhost:4000/" + _file,
        success: handleRes,
        error: function(res, status, error) {
            console.log(error)
        }
    });
}
  • Website: http://www.arbitrarytech.com
  • Reference: http://blog.tcs.de/post-file-to-s3-using-node