2024-11-24 19:02:40 +00:00
```markdown
2023-11-03 00:36:11 +00:00
# @push.rocks/smartbucket
2024-05-21 16:46:59 +00:00
2024-11-24 19:12:20 +00:00
A comprehensive TypeScript library for cloud-agnostic object storage offering bucket management, file operations, and advanced data streaming.
2019-07-07 08:48:24 +00:00
2024-04-14 15:22:27 +00:00
## Install
2024-11-24 19:02:40 +00:00
To install `@push.rocks/smartbucket` , ensure you have Node.js and npm installed. Then, run the following command in your project directory:
2024-04-14 15:22:27 +00:00
```bash
npm install @push .rocks/smartbucket --save
```
2024-11-24 19:02:40 +00:00
This command will add `@push.rocks/smartbucket` to your project's dependencies and install it along with its requirements in the `node_modules` directory.
2019-07-07 08:48:24 +00:00
## Usage
2024-11-24 19:12:20 +00:00
### Introduction
`@push.rocks/smartbucket` provides a robust set of features to manage cloud storage operations in a cloud-agnostic manner. By leveraging this library, you can seamlessly interact with object storage services like AWS S3, without being tied to any vendor-specific implementations. This library not only abstracts basic file operations but also integrates advanced capabilities such as metadata management, data streaming, file locking, and bucket policies, all through a simplified API.
2024-05-17 17:24:52 +00:00
### Table of Contents
2024-11-24 19:02:40 +00:00
2024-05-17 17:24:52 +00:00
1. [Setting Up ](#setting-up )
2024-11-24 19:02:40 +00:00
2. [Working with Buckets ](#working-with-buckets )
- [Creating a New Bucket ](#creating-a-new-bucket )
- [Listing Buckets ](#listing-buckets )
- [Deleting Buckets ](#deleting-buckets )
3. [File Operations in Buckets ](#file-operations-in-buckets )
2024-05-17 17:24:52 +00:00
- [Uploading Files ](#uploading-files )
- [Downloading Files ](#downloading-files )
- [Streaming Files ](#streaming-files )
2024-11-24 19:02:40 +00:00
- [Deleting Files ](#deleting-files )
4. [Directory Operations ](#directory-operations )
- [Listing Directories and Files ](#listing-directories-and-files )
- [Managing Files in Directories ](#managing-files-in-directories )
5. [Advanced Features ](#advanced-features )
2024-05-17 17:24:52 +00:00
- [Bucket Policies ](#bucket-policies )
2024-11-24 19:02:40 +00:00
- [Metadata Management ](#metadata-management )
- [File Locking ](#file-locking )
2024-11-24 19:12:20 +00:00
- [Trash Management ](#trash-management )
2024-11-24 19:02:40 +00:00
6. [Cloud Agnosticism ](#cloud-agnosticism )
2024-04-14 15:22:27 +00:00
### Setting Up
2024-11-24 19:12:20 +00:00
Begin by importing the necessary classes from the `@push.rocks/smartbucket` package into your TypeScript file. Create an instance of `SmartBucket` with your storage configuration:
2024-04-14 15:22:27 +00:00
```typescript
import {
SmartBucket,
Bucket,
Directory,
File
} from '@push.rocks/smartbucket';
const mySmartBucket = new SmartBucket({
accessKey: "yourAccessKey",
accessSecret: "yourSecretKey",
endpoint: "yourEndpointURL",
2024-11-24 19:02:40 +00:00
port: 443,
useSsl: true
2024-04-14 15:22:27 +00:00
});
```
2024-11-24 19:12:20 +00:00
Replace `"yourAccessKey"` , `"yourSecretKey"` , and `"yourEndpointURL"` with actual data specific to your cloud provider.
2024-11-24 19:02:40 +00:00
### Working with Buckets
2024-04-14 15:22:27 +00:00
2024-11-24 19:02:40 +00:00
#### Creating a New Bucket
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
Creating a bucket involves invoking the `createBucket` method. Note that bucket names are unique and follow the rules of the cloud provider:
2024-04-14 15:22:27 +00:00
```typescript
async function createBucket(bucketName: string) {
try {
2024-11-24 19:02:40 +00:00
const newBucket: Bucket = await mySmartBucket.createBucket(bucketName);
2024-04-14 15:22:27 +00:00
console.log(`Bucket ${bucketName} created successfully.`);
} catch (error) {
console.error("Error creating bucket:", error);
}
}
2024-11-24 19:02:40 +00:00
createBucket("myNewBucket");
2024-04-14 15:22:27 +00:00
```
2024-11-24 19:02:40 +00:00
#### Listing Buckets
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
While the library uses cloud-provider capabilities like AWS SDK to list existing buckets, `smartbucket` is aimed at simplifying content management within them.
2024-04-14 15:22:27 +00:00
2024-11-24 19:02:40 +00:00
#### Deleting Buckets
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
To delete a bucket, simply call the `removeBucket` function:
2024-04-14 15:22:27 +00:00
```typescript
2024-11-24 19:02:40 +00:00
async function deleteBucket(bucketName: string) {
try {
await mySmartBucket.removeBucket(bucketName);
console.log(`Bucket ${bucketName} deleted successfully.`);
} catch (error) {
console.error("Error deleting bucket:", error);
2024-04-14 15:22:27 +00:00
}
}
2024-11-24 19:12:20 +00:00
deleteBucket("anotherBucketName");
2024-04-14 15:22:27 +00:00
```
2024-11-24 19:02:40 +00:00
### File Operations in Buckets
2024-11-24 19:12:20 +00:00
SmartBucket offers a unified API to execute file-based operations efficiently.
2024-11-24 19:02:40 +00:00
#### Uploading Files
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
Upload a file using the `fastPut` method, specifying the bucket name, file path, and content:
2024-04-14 15:22:27 +00:00
```typescript
2024-11-24 19:02:40 +00:00
async function uploadFile(bucketName: string, filePath: string, fileContent: Buffer | string) {
const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
await bucket.fastPut({ path: filePath, contents: fileContent });
2024-11-24 19:12:20 +00:00
console.log(`File uploaded to ${filePath}`);
2024-04-14 15:22:27 +00:00
}
2024-11-24 19:02:40 +00:00
uploadFile("myBucket", "example.txt", "This is a sample file content.");
2024-04-14 15:22:27 +00:00
```
2024-11-24 19:02:40 +00:00
#### Downloading Files
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
Download files using `fastGet` . It retrieves the file content as a buffer:
2024-04-14 15:22:27 +00:00
```typescript
2024-11-24 19:02:40 +00:00
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());
2024-04-14 15:22:27 +00:00
}
2024-11-24 19:02:40 +00:00
downloadFile("myBucket", "example.txt");
2024-04-14 15:22:27 +00:00
```
2024-05-17 17:24:52 +00:00
#### Streaming Files
2024-11-24 19:12:20 +00:00
For large-scale applications, stream files without loading them fully into memory:
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
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."));
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
streamFile("myBucket", "largefile.txt");
2024-05-17 17:24:52 +00:00
```
2024-11-24 19:02:40 +00:00
#### Deleting Files
2024-11-24 19:12:20 +00:00
Delete files with precision using `fastRemove` :
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
async function deleteFile(bucketName: string, filePath: string) {
const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
await bucket.fastRemove({ path: filePath });
2024-11-24 19:12:20 +00:00
console.log(`File ${filePath} deleted.`);
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
deleteFile("myBucket", "example.txt");
2024-05-17 17:24:52 +00:00
```
2024-11-24 19:02:40 +00:00
### Directory Operations
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
Leverage directory functionalities to better organize and manage files within buckets.
2024-11-24 19:02:40 +00:00
#### Listing Directories and Files
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
Listing contents showcases a directory’ s structure and file contents:
2024-05-21 16:46:59 +00:00
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
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));
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
listDirectory("myBucket", "path/to/directory");
2024-05-17 17:24:52 +00:00
```
2024-11-24 19:02:40 +00:00
#### Managing Files in Directories
2024-11-24 19:12:20 +00:00
Additional functionalities allow file management, inclusive of handling sub-directories:
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
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()}`);
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
manageFilesInDirectory("myBucket", "myDir", "example.txt", "File content here");
2024-05-17 17:24:52 +00:00
```
### Advanced Features
2024-11-24 19:12:20 +00:00
The library’ s advanced features streamline intricate cloud storage workflows.
2024-05-17 17:24:52 +00:00
#### Bucket Policies
2024-11-24 19:12:20 +00:00
The library offers tools for maintaining consistent bucket policies across storage providers, assisting in defining access roles and permissions.
2024-05-17 17:24:52 +00:00
2024-11-24 19:02:40 +00:00
#### Metadata Management
2024-05-17 17:24:52 +00:00
2024-11-24 19:12:20 +00:00
Easily manage and store metadata by using the `MetaData` utility:
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
async function handleMetadata(bucketName: string, filePath: string) {
const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
2024-11-24 19:12:20 +00:00
const meta = await bucket.fastStat({ path: filePath });
2024-11-24 19:02:40 +00:00
console.log("Metadata:", meta.Metadata);
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
handleMetadata("myBucket", "example.txt");
2024-05-17 17:24:52 +00:00
```
2024-11-24 19:02:40 +00:00
#### File Locking
2024-11-24 19:12:20 +00:00
Prevent accidental writes by locking files:
2024-05-17 17:24:52 +00:00
```typescript
2024-11-24 19:02:40 +00:00
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.`);
2024-05-17 17:24:52 +00:00
}
2024-11-24 19:02:40 +00:00
lockFile("myBucket", "example.txt");
2024-05-17 17:24:52 +00:00
```
2024-11-24 19:12:20 +00:00
#### Trash Management
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
SmartBucket enables a safe deletion mode where files can be moved to a recycling bin, allowing for restoration:
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
```typescript
async function trashAndRestoreFile(bucketName: string, filePath: string) {
const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
const file: File = await bucket.getBaseDirectory().getFileStrict({ path: filePath });
// Move the file to trash
await file.delete({ mode: 'trash' });
console.log(`File ${filePath} moved to trash.`);
// Retrieve the file from the trash
const trashFile = await bucket.getTrash().getTrashedFileByOriginalName({ path: filePath });
await trashFile.restore();
console.log(`File ${filePath} restored from trash.`);
}
trashAndRestoreFile("myBucket", "example.txt");
2024-11-24 19:02:40 +00:00
```
2024-04-14 15:22:27 +00:00
2024-11-24 19:12:20 +00:00
### Cloud Agnosticism
`@push.rocks/smartbucket` supports a multitude of cloud providers, enhancing flexibility in adopting different cloud strategies without the need for extensive code rewrite. It offers a uniform interface allowing to perform operations seamlessly between different storage solutions such as AWS S3, Google Cloud Storage, and more. This aspect empowers organizations to align their storage decisions with business needs rather than technical constraints.
By following this guide, you should be well-equipped to handle cloud storage operations using the `@push.rocks/smartbucket` library. Diligently constructed code examples elucidate the extensive functionalities offered by the library, aligned with best practices in cloud storage. For a deeper dive into any specific feature, refer to the comprehensive documentation provided with the library and the official documentation of the cloud providers you are integrating with.
```
2024-04-14 15:22:27 +00:00
## 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
2020-05-17 15:57:12 +00:00
2024-04-14 15:22:27 +00:00
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.
2020-05-17 15:57:12 +00:00
2024-04-14 15:22:27 +00:00
### Company Information
2020-05-17 15:57:12 +00:00
2024-04-14 15:22:27 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2019-07-07 08:48:24 +00:00
2024-04-14 15:22:27 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2019-07-07 08:48:24 +00:00
2024-04-14 15:22:27 +00:00
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.