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

gcloud-storage-json-upload

v1.0.5

Published

Upload a file to Google Cloud Storage using the HTTP JSON api

Downloads

132

Readme

Google Cloud Storage Upload via HTTP JSON Api

This project makes it simple to upload a file to a Google Cloud Storage bucket using Google's HTTP JSON Api. This is an option you can use instead of having a dependency on the entire Google SDK toolchain, which can have some downsides.

Mainly, it's huge, over 80MB of node_modules. If you are packaging that into a deployable package such as an Electron app or an executable created with pkg then that is a huge dependency to pull in when all you want is to enable uploading a file.

Secondly, the authentication token generation for service accounts is useful as you can use this token elsewhere with Google's services.

Usage

Example

Firstly, see the simple-example/run_example.js file for a sample of how to use the code. Run it using

cd simple-example
node ./run_example.js --file=FILE_TO_UPLOAD --service_key=SERVICE_KEY_FILE

The SERVICE_KEY_FILE is a file containing your generated private key for a service account. You can generate a new one by going to the URL https://console.firebase.google.com/u/0/project/<YOUR PROJECT ID>/settings/serviceaccounts/adminsdk and clicking the Generate new private key button.

Code

There are two modules, auth and upload.

  • auth authenticates your application using the private key and service account email address. It returns a Promise. To use it, do the following:
const {auth} = require('gcloud-storage-json-upload');

const privateKey = ......
const serviceAccountEmail = ......

auth(privateKey, serviceAccountEmail).then(token => {
  // Cool, I have a token that lasts for an hour, I can do stuff with it!
})
  • upload uploads a file to the server. You pass it the source file path to upload, the token you received from the auth function, the storage ID of the Google Cloud storage bucket you are using and the content type. The storage ID is generally the same as the project_id field in the private key file. You can optionally tell it the content type of the file, e.g. "text/plain", "text/csv" etc, and the destination path to store the file in the bucket. These default to "text/plain" and the root folder with the source file name.
const {auth, upload} = require('gcloud-storage-json-upload');

const filePath = .....
const privateKey = ......
const serviceAccountEmail = ......
const storageID = ......


auth(privateKey, serviceAccountEmail).then(token => {
  upload(filePath, token, storageID, "text/csv", `my_csvs/newFile.csv`).then(() => {
    console.log('Sweet my file uploaded');
  }).catch((err) => {
     console.log('Uh oh, file upload failed with error',err);
  });
})

Two examples

There are two examples in this project.

  • A simple project that is run from the command line. See the simple-example folder.
  • A small NextJS app with an API endpoint and a React based page for calling that endpoint. See the nextjs-gcloud-auth-example folder.