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

gcs-signed-urls

v1.0.4

Published

Allow uploads directly from the browser to Google Cloud Storage while controlling access with Nodejs and signed urls.

Downloads

84

Readme

Google Cloud Storage Signed URLs for Node.js

I created this NPM module so that I can create web apps that allow users to upload directly to Google Cloud Storage. Using Signed URLs I am able to control access with Node.js and a private key. Because there is not a Node.js SDK for Google Cloud Storage, the cryptographic signatures were coded using Node.js's crypto module to Google's specification. This integration alleviates much of the maintenance required in storing and handing file uploads in web apps while at the same time not complicating the uploading process by acting as a middleman between the user and Google Cloud Storage.

NOTE: you need to create a google service account.

Simple example: http://gcs-signed-urls.herokuapp.com/.

For more advanced examples with xhr uploads, see https://github.com/sfarthin/crop-rotate-and-sample-in-browser.

Setup

npm install gcs-signed-urls

Creating a Service Account

  1. Visit the Google Developer Console
  2. Click credentials
  3. Create new client ID
  4. Create a service account

Incorperate service account.

  1. After you create a service account you should recieve a .p12 file. Convert the .p12 (key and certificate in one) to an ascii formatted .pem which can be read as text. Use this command:

    openssl pkcs12 -in *.p12 -out google-services-private-key.pem -nodes -clcerts

  2. The password is always "notasecret"

  3. Your all set! Now you will be able to run the example app.

Reference

First create your CloudStorage instance with your gcs information.

var CloudStorage = require("gcs-signed-urls")("/path/to/google-services-private-key.pem", "*****@developer.gserviceaccount.com", "my_bucket_name")

You can also store gcs information in environment variables so you do not need to specify it in your application.

var CloudStorage = require("gcs-signed-urls")()

Set variables like this:

$ export GOOGLE_SERICES_EMAIL=*****@developer.gserviceaccount.com
$ export GCS_STORAGE_BUCKET=my-bucket
$ export GCS_PRIVATE_KEY=`cat path/to/google-services-private-key.pem`

uploadRequest(filename, key, isAttachment, customFields)

This method creates an object representing the fields of an HTML form.

filename - Filename given to the file uploaded. Mime type is determined given the extension.

key - The Google Cloud key.

isAttachment - This sets content disposition to be an attachment, causing the browser to download the file (with the given filename) rather than show the file inside the browser.

customFields - Set custom "x-goog-meta-" headers.

<form action="http://my_bucket.storage.googleapis.com" method="post" enctype="multipart/form-data">
	<input type="text" name="key" value="<%=key%>">
	<input type="hidden" name="bucket" value="my_bucket">
	<input type="hidden" name="GoogleAccessId" value="*****@developer.gserviceaccount.com">
	<input type="hidden" name="policy" value="<%=fields.policy%>">
	<input type="hidden" name="signature" value="<%=fields.signature%>">

	<input name="file" type="file">
	<input type="submit" value="Upload">
</form>

upload(filename, key, isAttachment, customFields, callback)

Direct upload from Node.js using the same options as uploadRequest.

defaultAcl: function(acl, callback)

Takes an acl option and sets that as the default acl of an object. The options are: project-private private public-read public-read-write authenticated-read bucket-owner-read bucket-owner-full-control

See https://developers.google.com/storage/docs/accesscontrol#extension

cors: function(xml, callback)

Sets the xml cors policy

exisits: function(key, callback)

metaData: function(key, callback)

makePrivate: function(key, callback)

makePublic: function(key, callback)

getPublicUrl: function(key)

getPrivateUrl: function(key)

remove: function(key, callback)

Running tests or example

The tests and example require environoment variables to be set.

$ export GOOGLE_SERICES_EMAIL=*****@developer.gserviceaccount.com
$ export GCS_STORAGE_BUCKET=my-bucket
$ export GCS_PRIVATE_KEY=`cat google-services-private-key.pem`

To run test

npm test

To run example on port 3001

npm start

Deploying on Heroku

heroku create
heroku config:set GOOGLE_SERICES_EMAIL=***@developer.gserviceaccount.com
heroku config:set GCS_STORAGE_BUCKET=my bucket
heroku config:set GCS_PRIVATE_KEY=`cat google-services-private-key.pem`
git push heroku master