Giter VIP home page Giter VIP logo

any-cloud-storage's Introduction

any-cloud-storage

any-cloud-storage provides a common interface for file storage across multiple cloud providers including AWS S3, GCP, Firebase, Supabase, git and local file-system storage.

Interface

The library provides the FileStorage interface with the following methods:

  • saveFile(filePath, data: Buffer, { encoding: 'utf-8', contentType: 'text/plain' })
  • saveTextFile(filePath, data: string)
  • readFile(filePath) => Promise<Buffer>
  • readTextFile(filePath) => Promise<string>
  • deleteFile(filePath)
  • listFiles(directoryPath) => Promise<string[]>

Configuration

Each implementation can be configured using a nested map of string | number | boolean | array so that the configuration can be saved in a JSON/YAML file.

Examples:

  • { type: 'file', basePath }
  • { type: 'firebase', bucketUrl: 'gs://my-custom-bucket' }
  • { type: 'gcp', bucketName, apiEndpoint? }
  • { type: 'git', repoUrl, username, options?: { baseDir?, binary?, config? } }
  • { type: 's3', bucket }
  • { type: 'supabase', url, key, bucket }

Usage

Import createStorage and call it with your config to create a storage instance.

import createStorage from "any-cloud-storage";

const config = { type: "file", basePath: "/tmp" };
// const config = {
//   type: 's3',
//   bucket: 'my-bucket',
//   region: 'us-west-2',
//   // other AWS S3 configuration options...
// };

const storage = createStorage(config);

Alternatively, you directly import the implementation you want to use:

import LocalFileStorage from "any-cloud-storage/implementations/LocalFileStorage";

const storage = new LocalFileStorage("/tmp");

Now you can use the storage instance to read/write files:

const filePath = "path/to/my/file.txt";
const text = "Hello, world!";

// Save a file
await storage.saveTextFile(filePath, text);
// or await storage.saveFile(filePath, Buffer.from(text, 'utf-8'));

// Read a file
const readDate = await storage.readTextFile(filePath);
// or const readData = (await storage.readFile(filePath)).toString('utf-8');
console.log(readData); // 'Hello, world!'

// List files
const files = await storage.listFiles("path/to/my");
console.log(files); // ['file.txt']

// Delete a file
await storage.deleteFile(filePath);

Implementations

The library includes implementations for the following cloud providers:

  • Firebase (FirebaseClientStorage)
  • Google Cloud Platform (GcpStorage)
  • Git (GitStorage)
  • Local file system (LocalFileStorage)
  • AWS (S3FileStorage)
  • Supabase (SupabaseStorage)

any-cloud-storage's People

Contributors

nalbion avatar

Stargazers

 avatar  avatar Rinat Arsaev avatar Olola Birka avatar  avatar Javed Khan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.