A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.
Go to file
2024-11-24 20:02:41 +01:00
.vscode fix(core): update 2022-03-31 01:45:46 +02:00
test feat(core): Enhanced directory handling and file restoration from trash 2024-11-24 19:56:12 +01:00
ts fix(documentation): Updated keywords and description for clarity and consistency. 2024-11-24 20:02:40 +01:00
.gitignore fix(core): update 2020-05-17 15:57:12 +00:00
.gitlab-ci.yml fix(core): update 2023-11-03 01:36:11 +01:00
changelog.md fix(documentation): Updated keywords and description for clarity and consistency. 2024-11-24 20:02:40 +01:00
npmextra.json fix(documentation): Updated keywords and description for clarity and consistency. 2024-11-24 20:02:40 +01:00
package-lock.json 3.3.2 2024-11-24 20:02:41 +01:00
package.json 3.3.2 2024-11-24 20:02:41 +01:00
pnpm-lock.yaml feat(bucket): Enhanced SmartBucket with trash management and metadata handling 2024-11-24 02:25:08 +01:00
qenv.yml fix(test): Update endpoint configuration in tests to use environment variable 2024-07-04 18:39:27 +02:00
readme.hints.md update tsconfig 2024-04-14 17:22:27 +02:00
readme.md fix(documentation): Updated keywords and description for clarity and consistency. 2024-11-24 20:02:40 +01:00
tsconfig.json fix(metadata): Fix metadata handling to address type assertion and data retrieval. 2024-11-18 11:24:11 +01:00

# @push.rocks/smartbucket

A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.

## Install

To install `@push.rocks/smartbucket`, ensure you have Node.js and npm installed. Then, run the following command in your project directory:

```bash
npm install @push.rocks/smartbucket --save

This command will add @push.rocks/smartbucket to your project's dependencies and install it along with its requirements in the node_modules directory.

Usage

Table of Contents

  1. Setting Up
  2. Working with Buckets
  3. File Operations in Buckets
  4. Directory Operations
  5. Advanced Features
  6. Cloud Agnosticism

Setting Up

Start by setting up @push.rocks/smartbucket in a TypeScript file, ensuring your project uses ECMAScript modules:

import {
  SmartBucket,
  Bucket,
  Directory,
  File
} from '@push.rocks/smartbucket';

const mySmartBucket = new SmartBucket({
  accessKey: "yourAccessKey",
  accessSecret: "yourSecretKey",
  endpoint: "yourEndpointURL",
  port: 443,
  useSsl: true
});

Replace "yourAccessKey", "yourSecretKey", and "yourEndpointURL" with appropriate values for your cloud storage service.

Working with Buckets

Creating a New Bucket

To create a new bucket, use the createBucket method. Remember that bucket names must be unique across the storage service:

async function createBucket(bucketName: string) {
  try {
    const newBucket: Bucket = await mySmartBucket.createBucket(bucketName);
    console.log(`Bucket ${bucketName} created successfully.`);
  } catch (error) {
    console.error("Error creating bucket:", error);
  }
}

createBucket("myNewBucket");

Listing Buckets

SmartBucket allows you to manage buckets but relies on the cloud provider's SDK for listing them.

Deleting Buckets

You can delete a bucket using the removeBucket method:

async function deleteBucket(bucketName: string) {
  try {
    await mySmartBucket.removeBucket(bucketName);
    console.log(`Bucket ${bucketName} deleted successfully.`);
  } catch (error) {
    console.error("Error deleting bucket:", error);
  }
}

deleteBucket("myNewBucket");

File Operations in Buckets

Uploading Files

To upload a file to a bucket, use the fastPut method:

async function uploadFile(bucketName: string, filePath: string, fileContent: Buffer | string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  await bucket.fastPut({ path: filePath, contents: fileContent });
  console.log(`File uploaded to ${bucketName} at ${filePath}`);
}

uploadFile("myBucket", "example.txt", "This is a sample file content.");

Downloading Files

Retrieve files using the fastGet method:

async function downloadFile(bucketName: string, filePath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const content: Buffer = await bucket.fastGet({ path: filePath });
  console.log("Downloaded content:", content.toString());
}

downloadFile("myBucket", "example.txt");

Streaming Files

For large files, use streams:

async function streamFile(bucketName: string, filePath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const stream = await bucket.fastGetStream({ path: filePath }, "nodestream");
  stream.on('data', chunk => console.log("Chunk:", chunk.toString()));
  stream.on('end', () => console.log("Download completed."));
}

streamFile("myBucket", "largefile.txt");

Deleting Files

Remove files with the fastRemove method:

async function deleteFile(bucketName: string, filePath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  await bucket.fastRemove({ path: filePath });
  console.log(`File ${filePath} deleted from ${bucketName}.`);
}

deleteFile("myBucket", "example.txt");

Directory Operations

Listing Directories and Files

You can navigate and list files in directories within a bucket:

async function listDirectory(bucketName: string, directoryPath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const baseDirectory: Directory = await bucket.getBaseDirectory();
  const targetDirectory = await baseDirectory.getSubDirectoryByName(directoryPath);

  console.log('Directories:');
  (await targetDirectory.listDirectories()).forEach(dir => console.log(dir.name));

  console.log('Files:');
  (await targetDirectory.listFiles()).forEach(file => console.log(file.name));
}

listDirectory("myBucket", "path/to/directory");

Managing Files in Directories

Upload, download, and manage files using directory abstractions:

async function manageFilesInDirectory(bucketName: string, directoryPath: string, fileName: string, content: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const baseDirectory: Directory = await bucket.getBaseDirectory();
  const directory = await baseDirectory.getSubDirectoryByName(directoryPath) ?? baseDirectory;

  await directory.fastPut({ path: fileName, contents: content });
  console.log(`File ${fileName} created in ${directoryPath}`);

  const fileContent = await directory.fastGet({ path: fileName });
  console.log(`Content of ${fileName}: ${fileContent.toString()}`);
}

manageFilesInDirectory("myBucket", "myDir", "example.txt", "File content here");

Advanced Features

Bucket Policies

SmartBucket facilitates bucket policy management, depending on the cloud SDK's capabilities.

Metadata Management

You can retrieve and manipulate object metadata, employing it for additional data storage:

async function handleMetadata(bucketName: string, filePath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const meta = await bucket.smartbucketRef.s3Client.send(new plugins.s3.HeadObjectCommand({
    Bucket: bucket.name,
    Key: filePath,
  }));
  console.log("Metadata:", meta.Metadata);
}

handleMetadata("myBucket", "example.txt");

File Locking

Lock files to prevent changes:

async function lockFile(bucketName: string, filePath: string) {
  const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
  const file: File = await bucket.getBaseDirectory().getFileStrict({ path: filePath });
  await file.lock({ timeoutMillis: 600000 }); // Lock for 10 minutes
  console.log(`File ${filePath} locked.`);
}

lockFile("myBucket", "example.txt");

Cloud Agnosticism

@push.rocks/smartbucket supports multiple cloud providers, enhancing flexibility in cloud strategies without significant code changes. Adjust configurations as necessary for different providers, as services like AWS S3 or Google Cloud Storage might offer unique features beyond SmartBucket's unified interface.

This guide demonstrates various operations with @push.rocks/smartbucket. Always refer to the comprehensive documentation and cloud provider details to fully leverage the library's capabilities.


This readme provides detailed documentation on using the `@push.rocks/smartbucket` module, demonstrating its capabilities through comprehensive examples and use cases. Each section is designed to guide a user through basic to more complex operations, ensuring a complete presentation of the library's features.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.