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-storage-utils-st

v1.0.3

Published

A utility package for Google Cloud Platform Storage operations

Downloads

15

Readme

GCP Storage Utils

A utility package for Google Cloud Platform Storage operations using a functional approach.

Installation

npm install gcp-storage-utils

Usage

First, ensure you have set up a Google Cloud Platform project and have a service account key file.

Import the necessary types and functions:

import { createGCPStorageUtils, GCPConfig } from "gcp-storage-utils";

Create a configuration object:

const config: GCPConfig = {
  bucketName: "your-bucket-name",
  keyFilename: "/path/to/your/keyfile.json",
  maxFileSize: 10 * 1024 * 1024, // 10MB
  allowedFileTypes: ["image/jpeg", "image/png", "application/pdf"],
};

Initialize the GCP Storage Utils:

async function main() {
  const gcpUtils = await createGCPStorageUtils(config);

  // Now you can use gcpUtils to perform various operations
}

main().catch(console.error);

Use the utility functions:

Upload a file

const publicUrl = await gcpUtils.uploadFile(
  "/path/to/local/file.jpg",
  "destination-filename.jpg"
);
console.log("File uploaded:", publicUrl);

List files

const files = await gcpUtils.listFiles();
console.log("Files in bucket:", files);

Read a file

const fileContent = await gcpUtils.readFile("filename.txt");
console.log("File content:", fileContent.toString());

Update a file

const updatedUrl = await gcpUtils.updateFile(
  "existing-file.txt",
  "/path/to/new/file.txt"
);
console.log("File updated:", updatedUrl);

Delete a file

await gcpUtils.deleteFile("file-to-delete.txt");
console.log("File deleted");

Get a signed URL for upload

const uploadUrl = await gcpUtils.getSignedUrlForUpload(
  "new-file.jpg",
  "image/jpeg",
  30
);
console.log("Upload URL:", uploadUrl);

Get a signed URL for download

const downloadUrl = await gcpUtils.getSignedUrlForDownload(
  "existing-file.jpg",
  30
);
console.log("Download URL:", downloadUrl);

Move a file

await gcpUtils.moveFile("source-file.txt", "destination-file.txt");
console.log("File moved");

Copy a file

await gcpUtils.copyFile("source-file.txt", "copy-file.txt");
console.log("File copied");

Error Handling

All functions throw errors when operations fail. It's recommended to use try-catch blocks or .catch() on the promises to handle potential errors:

try {
  await gcpUtils.uploadFile("/path/to/file.jpg", "uploaded-file.jpg");
} catch (error) {
  console.error("Upload failed:", error.message);
}

Configuration Options

The GCPConfig interface allows you to customize the behavior of the utility:

  • bucketName (required): The name of your GCP Storage bucket
  • keyFilename (required): Path to your GCP service account key file
  • maxFileSize (optional): Maximum allowed file size in bytes (default: 5MB)
  • allowedFileTypes (optional): Array of allowed MIME types (default: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf'])

Best Practices

  • Always use environment variables or secure secret management for sensitive information like the keyFilename.
  • Implement proper error handling in your application.
  • Use TypeScript for better type checking and developer experience.
  • Regularly update the package to get the latest features and security updates.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.