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

any-bucket

v0.1.6

Published

File storage abstraction layer

Downloads

76

Readme

Any Bucket

This library provides implementations for interacting with cloud storage services such as Amazon S3, Azure Blob Storage, Google Cloud Storage, and local file system storage. It abstracts away the specific details of each provider and provides a unified interface for common storage operations.

Installation

To install the library, you can use npm or yarn:

npm install any-bucket

or

yarn add any-bucket

Usage

All providers implement the Bucket interface, which provides the following methods:

import { BufferEncodingOption } from 'fs';

interface Bucket {
  /**
   * List all files in the bucket.
   */
  list(folder?: string): Promise<string[]>;

  /**
   * Check if a file exists in the bucket.
   *
   * @param key The path to the file.
   */
  has(key: string): Promise<boolean>;

  /**
   * Get the contents of a file.
   * If no encoding is specified, the file contents will be returned as a buffer.
   *
   * @param key The path to the file.
   * @param encoding The encoding to use ('utf8', etc).
   */
  get(key: string, encoding?: BufferEncodingOption): Promise<Buffer | string>;

  /**
   * Set the contents of a file.
   *
   * @param key The path to the file.
   * @param content The contents of the file.
   */
  put(key: string, content: Uint8Array | string): Promise<void>;

  /**
   * Delete a file.
   *
   * @param key The path to the file.
   */
  delete(key: string): Promise<void>;
}

Local Storage

The LocalBucket class provides an implementation for local file system storage. It can be used as follows:

import LocalBucket from 'any-bucket/local';

const localBucket = new LocalBucket('/path/to/base/folder');

// Example usage
const files = await localBucket.list();
console.log(files);

const hasFile = await localBucket.has('file.txt');
console.log(hasFile);

const fileContent = await localBucket.get('file.txt');
console.log(fileContent);

await localBucket.put('file.txt', 'Hello, World!');

await localBucket.delete('file.txt');

Amazon S3

npm install @aws-sdk/client-s3

The S3Bucket class provides an implementation for Amazon S3 storage. It requires the AWS SDK to be installed and configured. Here's an example of how to use it:

import { S3 } from '@aws-sdk/client-s3';
import S3Bucket from 'any-bucket/s3';

const s3Client = new S3({ /* configure AWS SDK options */ });
const s3Bucket = new S3Bucket(s3Client, 'my-bucket');

// Example usage
const files = await s3Bucket.list();
console.log(files);

const hasFile = await s3Bucket.has('file.txt');
console.log(hasFile);

const fileContent = await s3Bucket.get('file.txt');
console.log(fileContent);

await s3Bucket.put('file.txt', 'Hello, S3!');

await s3Bucket.delete('file.txt');

Instead of a bucket name you can pass a bucket name and a path to the constructor. This will prepend the path name to all file paths. For example:

const s3Bucket = new S3Bucket(s3Client, 'my-bucket/my-folder');

Azure Blob Storage

npm install @azure/storage-blob

The AzureBucket class provides an implementation for Azure Blob Storage. It requires the @azure/storage-blob package to be installed and authenticated. Here's an example of how to use it:

import { BlobServiceClient } from '@azure/storage-blob';
import AzureBucket from 'any-bucket/azure';

const connectionString = '<your-connection-string>';
const azureClient = BlobServiceClient.fromConnectionString(connectionString);
const azureBucket = new AzureBucket(azureClient, 'my-container');

// Example usage
const files = await azureBucket.list();
console.log(files);

const hasFile = await azureBucket.has('file.txt');
console.log(hasFile);

const fileContent = await azureBucket.get('file.txt');
console.log(fileContent);

await azureBucket.put('file.txt', 'Hello, Azure!');

await azureBucket.delete('file.txt');

Instead of a bucket name you can pass a bucket name and a path to the constructor. This will prepend the path name to all file paths. For example:

const azureBucket = new AzureBucket(azureClient, 'my-container/my-folder');

Google Cloud Storage

npm install @google-cloud/storage

The GCSBucket class provides an implementation for Google Cloud Storage. It requires the @google-cloud/storage package to be installed and authenticated. Here's an example of how to use it:

import { Storage } from '@google-cloud/storage';
import GCSBucket from 'any-bucket/gcs';

const storage = new Storage({ /* configure authentication options */ });
const gcsBucket = new GCSBucket(storage, 'my-bucket');

// Example usage
const files = await gcsBucket.list();
console.log(files);

const hasFile = await gcsBucket.has('file.txt');
console.log(hasFile);

const fileContent = await gcsBucket.get('file.txt');
console.log(fileContent);

await gcsBucket.put('file.txt', 'Hello, GCS!');

await gcsBucket.delete('file.txt');

Instead of a bucket name you can pass a bucket name and a path to the constructor. This will prepend the path name to all file paths. For example:

const gcsBucket = new GCSBucket(storage, 'my-bucket/my-folder');

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please create an issue or submit a pull request on the GitHub repository.

License

This library is licensed under the MIT License.