1.0.3 • Published 10 months ago

gcp-storage-utils-st v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

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.

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago