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

gcp-bucket

v1.4.2

Published

A versatile library engineered to seamlessly manage file updates and perform image resizing tasks utilizing Google Cloud Platform's robust Bucket Storage infrastructure.

Downloads

70

Readme

GCP Bucket Library

The GCP Bucket Library facilitates seamless interactions with Google Cloud Storage buckets. It allows the uploading of files from various data types like strings, blobs, or buffers. Additionally, if the file to be uploaded is an image, the library provides the capability to resize the image into multiple dimensions as specified.

Getting Started

First, you need to initialize an instance of the GCPBucket class.

// Initialize your GCPBucket instance
const gcpBucket = new GCPBucket({
  bucketName: 'your-bucket-name',
  firebaseApp: <your firebase-admin instance>,
  chunkSize: 1024, // Optional chuck size to upload
  encryptKey: "key" // Optional An AES-256 encryption key.
});

Examples

Example 1: Uploading a Simple File (Any Type)

// Define the folder and file name for the upload
const folderName = 'uploads';
const fileName = file.originalname;

// Prepare file content
const fileContent = {
  folderName,
  fileName,
  fileData: file.buffer,
};

// Upsert the file to GCP
const result = await gcpBucket.upsertFiles(fileContent);

// The result will be
// [
//     {
//         "fileUrl": "file-url",
//         "filePath": "file-path",
//         "fileName": "file-name",
//         "fileType": "file-type",
//         "fileContentType": "file-content-type"
//     }
// ]

Example 2: Uploading and Resizing Images

// Define the folder and file name for the upload
const folderName = 'uploads';
const fileName = file.originalname;

// Prepare file content
const fileContent = {
  folderName,
  fileName,
  fileData: file.buffer,
  resizeOptions: [
    {
      height: 400,
      width: 400,
      fileName: "MediumImgFile"
      fit: 'inside',
    },
    { height: 200, width: 200, fileResizePrefix: 'small-', fit: 'inside' },
  ],
};

// Upsert the file to GCP
const result = await gcpBucket.upsertFiles(fileContent, callbackProcessPercentage);

callbackProcessPercentage is a callback function that gets invoked with the file path and the percentage of the resize process.

Method: deleteFile

Deleting a File

Parameters:

  • filePath (string) : The file path in the bucket.
await gcpBucket.deleteFile(filePath);

Method: download

Downloading a File

Parameters:

  • filePath (string) : The file path in the bucket.
await gcpBucket.download(filePath);

Method: isImage

Checking if a File is an Image

Parameters:

  • fileData (TFileContent['fileData']) : The image data which can be a Base64 string, Blob, or Buffer.
await gcpBucket.isImage(fileData);

Method: getImageSizeByFactor

This method resizes an image by a specified scale factor and returns the new dimensions along with a buffer of the resized image.

Parameters:

  • data (TFileContent['fileData']): The image data which can be a Base64 string, Blob, or Buffer.
  • scaleFactor (number): The scaling factor to resize the image. For example, a scaleFactor of 0.5 will reduce the image dimensions by 50%.
  • imgFit (keyof sharp.FitEnum | optional): The fit strategy for sharp to follow when resizing the image. Default is undefined.

Returns:

An object containing:

  • width (number): The new width of the resized image.
  • height (number): The new height of the resized image.
  • buffer (Buffer): A buffer of the resized image.

Usage:

const result = await gcpBucket.getImageSizeByFactor(fileData, 0.5, 'inside');

Method: getImageMetadata

This method retrieves and returns metadata of the specified image.

Parameters:

  • data (TFileContent['fileData']): The image data which can be a Base64 string, Blob, or Buffer.

Returns:

A Promise resolving to a sharp.Metadata object containing metadata information of the image.

Usage:

const metadata = await gcpBucket.getImageMetadata(fileData);

Method: getImage

This method return a new image with other size.

Parameters:

  • data (TFileContent['fileData']): The image data which can be a Base64 string, Blob, or Buffer.
  • newSize (TResizeOptions): The new image dimensions.

Returns:

  • buffer (Buffer): A buffer of the resized image.

Usage:

const metadata = await gcpBucket.getImage(fileData, {
  height: 100,
  width: 100,
  fit: 'cover',
});