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

@tweakly/multer-gcs-storage

v0.9.0

Published

A Google Cloud Storage (buckets) backend for multer

Downloads

70

Readme

multer-gcs-storage

A flexible GCS storage backend for multer

last commit npm package link MIT License

Introduction

This is a storage engine for multer that stores files in Google Cloud Storage buckets.

This package is primarily based on multer-cloud-storage, but with support for multiple buckets and configuration options for local testing.

In addition it has two extension methods compared with the multer interface: downloadFile and deleteFile.

Getting started

This package can be installed by your favourite package manager, e.g. pnpm.

pnpm add @tweakly/i18n-node-gcs-backend

Wiring up:

import multer from "multer";
import {MulterGcsStorage} from "@tweakly/multer-gcs-storage";

const upload = multer({
    storage: new MulterGcsStorage({
        bucketId: (req, file) => {
            return "file-upload-bucket";
        },
        destination: (req, file, cb) => {
            cb(null, "uploads");
        },
        filename: (req, file, cb) => {
            cb(null, file.originalname);
        }
    }),
});

Extension API

downloadFile

This method is used to download a file from the bucket. The file will be added as an attachment to the response, and the contents written to the response stream.

 async (bucketName: string, bucketFileName: string,
                  writeFileName: string, res: ExpressResponse): Promise<void>

deleteFile

This method is used to delete a file from the bucket.

 async (bucketName: string, bucketFileName: string): Promise<void>

Options

Note: Environment variables have precedence over configured variables where both exists.

| Parameter | Description | Environment Variable / .env | Options | |-----------------------------|-------------------------------------------------|-----------------------------------------------|------------------------------------| | Google Project | Project Name in which the Bucket resides | STORAGE_GCP_PROJECT | googleProject | | Credentials path (optional) | Provide your own google application credentials | STORAGE_GOOGLE_APPLICATION_CREDENTIALS_PATH | googleApplicationCredentialsPath |

The credentials used to connect to the GCS bucket needs to have the storage.buckets.get permission.

All options specific to multer-gcs-storage that can be provided are shown below.

{
  // Can be one of the values defined in Predefined ACLs. Defaults to 'private', but is not set if
  // uniformBucketLevelAccess is enabled.  
  acl: "private",

  // A string or a function that returns a string which represents the bucket id to be used for the given file.    
  // Required
  bucketId: "file-upload-bucket",
  
  // A function that returns the content type to set for the file. Default function returns file.mimetype.      
  contentType: (req, file) => {
    return file.mimetype;
  },      
        
  // A string or a function that gives the destination path for the file. Defaults to empty string     
  destination: (req, file, cb) => {
    cb(null, "uploads");
  },
        
  // A function that returns the filename to be used for the file. Defaults to {uuidv4}_{originalname}.      
  filename: (req, file, cb) => {
    cb(null, file.originalname);
  },      
        
  // A boolean that determines if the filename should be hidden in the file object. Defaults to false. If set, 
  // the filename will not be an uuid, and content type undefined.          
  hideFilename: false,      
        
  // If this option is set on the bucket, then it must be set here too. Otherwise an error will
  // be returned from GCS. Defaults to false. See also acl
  uniformBucketLevelAccess: false,      
    
  // The id of the Google project where the bucket is. This can also be set by env variable
  // STORAGE_GCP_PROJECT. It is required in one of the forms.  
  googleProject: "the-project-id",
        
  // The application credentials needed to connect to the GCP Storage bucket. This can also be set by env
  // variable STORAGE_GOOGLE_APPLICATION_CREDENTIALS_PATH.
  // NOTE: When deployed in GCP, credentials will if not provided be picked up from the
  //   cloud execution environment by the underlying client libraries.      
  googleApplicationCredentialsPath: "/somepath/credentials.json",
        
   // The API endpoint to connect to. Primarily useful for local testing, using e.g. fake-gcs-server.      
   // Optional     
   apiEndpoint: "https://storage.googleapis.com"
}