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-multer

v1.2.1

Published

This module can be used to add file uploading functionality to your server by using multer and google cloud server

Downloads

12

Readme

gcs-multer

This is a very simple module that can be used to add file-upload funcionality to your app. This module is the combination of the "multer" and "google-apis". Aim of this project is to help people to add file-upload functionality in a very easy way.

Installation

npm install gcs-multer

Creating an Instance of gcs-multer

For creating an instance of gcs-multer, you need to provide following three properties. These three properties must be given to enable gcs-multer to access google cloud storage

const gcs_multer = require("gcs-multer");

const gcsm = new gcs_multer({
  project_id: "wired-height-276615",
  key_file: "./wired-height-276615-c511781c0400.json",
  default_bucket: "oncoev_resources",
  max_size: 1024 * 1024 * 16, //optional
  is_public: false, //optional
});

File uploading

For this operation, gcs-multer works as a middleware. After the uploading operation finished, the original file name and google-cloud-storage_id of the file will be attached to the request object. Later, this file can be reached by using this gcs_id.

Since these files are uploaded to the cloud with automatically generated unique ids, the original name of the file can be needed when reaching the file. For a clearer explanation, read the downloading part as well.

Also, the request should be a multipart request which includes a file with the field name 'file'

// Listener for the progress of the upload operation
const action = (info) => {
  console.log(info); // this function can be used to see the progress 
};

// Add gcs-multer as a middleware
app.post("/", gcsm.singleFile(action), (req, res, next) => {
  const name = req.filename;
  const id = req.gcs_id;

  console.log(`uploaded file          : ${name}`);
  console.log(`uploded file's gcs_id  : ${id}`);
});

In the example above, "action()" function is called in each step of the upload operation. Following is the console logs of this operation

upload_console.png

upload_console_logs

This feature is added to the project to enable developers to reflect the progress to the user while the uploading operation continues.

File Downloading

The following is the download operation. gcs-multer generates a signed url for the file which can be used before the expiration date. In the following example, the url will redirect user to the download link and when the user downloads the file the file name will be prompted as the "original_file_name" to the user. User can use this url for the following 1 hour

app.get("/:id", (req, res, next) => {

  let expiresAt = Date.now() + 1000*60*60
  gcsm
    .getSignedURL(req.params.id, "original_file_name", expiresAt)
    .then((url) => {
      //the request is redirected to the download link
      res.redirect(url);
    })
    .catch((err) => {
      //handle error
    });
});

File Stream

The files can also be reached as streams. Following is an example of this operation.

let stream = gcsm.getFileStream("A6JrCdJpo");

stream.pipe(process.stdout);

Removing Files

Files can be deleted from the cloud simply by calling the deleteFile function.

gcsm.deleteFile("A6JrCdJpo");


Author

Nazmi Yılmaz