diff --git a/changelog.md b/changelog.md index f2775f9..657a8e1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2024-11-24 - 3.3.2 - fix(documentation) +Updated keywords and description for clarity and consistency. + +- Modified keywords and description in package.json and npmextra.json. +- Enhanced readme.md file structure and examples + ## 2024-11-24 - 3.3.1 - fix(File) Fixed issue with file restore metadata operations. diff --git a/npmextra.json b/npmextra.json index ac1a69b..2e1b573 100644 --- a/npmextra.json +++ b/npmextra.json @@ -8,28 +8,32 @@ "githost": "code.foss.global", "gitscope": "push.rocks", "gitrepo": "smartbucket", - "description": "A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.", + "description": "A TypeScript library facilitating cloud-agnostic object storage with capabilities such as bucket management, file operations, directory management, and advanced data streaming functionalities.", "npmPackagename": "@push.rocks/smartbucket", "license": "MIT", "keywords": [ "TypeScript", "cloud storage", "object storage", - "bucket creation", - "file management", + "bucket management", + "file operations", "directory management", "data streaming", "multi-cloud", - "API", - "unified storage", "S3", "minio", + "API", + "unified storage", "file locking", - "metadata", + "metadata management", "buffer handling", - "access key", - "secret key", - "cloud agnostic" + "access control", + "cloud agnostic", + "data streaming", + "file transfer", + "data management", + "streaming", + "environment configuration" ] } }, diff --git a/package.json b/package.json index 95a7d16..c93c223 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@push.rocks/smartbucket", "version": "3.3.1", - "description": "A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.", + "description": "A TypeScript library facilitating cloud-agnostic object storage with capabilities such as bucket management, file operations, directory management, and advanced data streaming functionalities.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "type": "module", @@ -49,25 +49,29 @@ "TypeScript", "cloud storage", "object storage", - "bucket creation", - "file management", + "bucket management", + "file operations", "directory management", "data streaming", "multi-cloud", - "API", - "unified storage", "S3", "minio", + "API", + "unified storage", "file locking", - "metadata", + "metadata management", "buffer handling", - "access key", - "secret key", - "cloud agnostic" + "access control", + "cloud agnostic", + "data streaming", + "file transfer", + "data management", + "streaming", + "environment configuration" ], "homepage": "https://code.foss.global/push.rocks/smartbucket", "repository": { "type": "git", "url": "https://code.foss.global/push.rocks/smartbucket.git" } -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 9e095ba..b4e117f 100644 --- a/readme.md +++ b/readme.md @@ -1,41 +1,44 @@ +```markdown # @push.rocks/smartbucket -A TypeScript library for cloud-independent object storage, providing features like bucket creation, file and directory management, and data streaming. +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`, you need to have Node.js and npm (Node Package Manager) installed. If they are installed, you can add `@push.rocks/smartbucket` to your project by running the following command in your project's root directory: +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 download and install `@push.rocks/smartbucket` along with its required dependencies into your project's `node_modules` directory and save it as a dependency in your project's `package.json` file. +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 -`@push.rocks/smartbucket` is a TypeScript module designed to provide simple cloud-independent object storage functionality. It wraps various cloud storage providers such as AWS S3, Google Cloud Storage, and others, offering a unified API to manage storage buckets and objects within those buckets. - -In this guide, we will delve into the usage of SmartBucket, covering its full range of features from setting up the library to advanced usage scenarios. - ### Table of Contents + 1. [Setting Up](#setting-up) -2. [Creating a New Bucket](#creating-a-new-bucket) -3. [Listing Buckets](#listing-buckets) -4. [Working with Files](#working-with-files) +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) - [Uploading Files](#uploading-files) - [Downloading Files](#downloading-files) - - [Deleting Files](#deleting-files) - [Streaming Files](#streaming-files) -5. [Working with Directories](#working-with-directories) -6. [Advanced Features](#advanced-features) + - [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) - [Bucket Policies](#bucket-policies) - - [Object Metadata](#object-metadata) - - [Cloud Agnostic](#cloud-agnostic) + - [Metadata Management](#metadata-management) + - [File Locking](#file-locking) +6. [Cloud Agnosticism](#cloud-agnosticism) ### Setting Up -First, ensure you are using ECMAScript modules (ESM) and TypeScript in your project for best compatibility. Here's how to import and initialize SmartBucket in a TypeScript file: +Start by setting up `@push.rocks/smartbucket` in a TypeScript file, ensuring your project uses ECMAScript modules: ```typescript import { @@ -49,264 +52,200 @@ const mySmartBucket = new SmartBucket({ accessKey: "yourAccessKey", accessSecret: "yourSecretKey", endpoint: "yourEndpointURL", - port: 443, // Default is 443, can be customized for specific endpoint - useSsl: true // Defaults to true + port: 443, + useSsl: true }); ``` -Make sure to replace `"yourAccessKey"`, `"yourSecretKey"`, and `"yourEndpointURL"` with your actual credentials and endpoint URL. The `port` and `useSsl` options are optional and can be omitted if the defaults are acceptable. +Replace `"yourAccessKey"`, `"yourSecretKey"`, and `"yourEndpointURL"` with appropriate values for your cloud storage service. -### Creating a New Bucket +### Working with Buckets -To create a new bucket: +#### Creating a New Bucket + +To create a new bucket, use the `createBucket` method. Remember that bucket names must be unique across the storage service: ```typescript async function createBucket(bucketName: string) { try { - const myBucket: Bucket = await mySmartBucket.createBucket(bucketName); + const newBucket: Bucket = await mySmartBucket.createBucket(bucketName); console.log(`Bucket ${bucketName} created successfully.`); } catch (error) { console.error("Error creating bucket:", error); } } -// Use the function -createBucket("exampleBucket"); +createBucket("myNewBucket"); ``` -Bucket names must be unique across the storage service. +#### Listing Buckets -### Listing Buckets +SmartBucket allows you to manage buckets but relies on the cloud provider's SDK for listing them. -Currently, SmartBucket does not include a direct method to list all buckets, but you can access the underlying client provided by the cloud storage SDK to perform such operations, depending on the SDK's capabilities. +#### Deleting Buckets -### Working with Files - -#### Uploading Files - -To upload an object to a bucket: +You can delete a bucket using the `removeBucket` method: ```typescript -async function uploadFile(bucketName: string, filePath: string, fileContent: Buffer | string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - await myBucket.fastPut({ path: filePath, contents: fileContent }); - console.log(`File uploaded to ${bucketName} at ${filePath}`); - } else { - console.error(`Bucket ${bucketName} does not exist.`); +async function deleteBucket(bucketName: string) { + try { + await mySmartBucket.removeBucket(bucketName); + console.log(`Bucket ${bucketName} deleted successfully.`); + } catch (error) { + console.error("Error deleting bucket:", error); } } -// Use the function -uploadFile("exampleBucket", "path/to/object.txt", "Hello, world!"); +deleteBucket("myNewBucket"); +``` + +### File Operations in Buckets + +#### Uploading Files + +To upload a file to a bucket, use the `fastPut` method: + +```typescript +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 -To download an object: +Retrieve files using the `fastGet` method: ```typescript async function downloadFile(bucketName: string, filePath: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - const fileContent: Buffer = await myBucket.fastGet({ path: filePath }); - console.log("Downloaded file content:", fileContent.toString()); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } + const bucket: Bucket = await mySmartBucket.getBucketByName(bucketName); + const content: Buffer = await bucket.fastGet({ path: filePath }); + console.log("Downloaded content:", content.toString()); } -// Use the function -downloadFile("exampleBucket", "path/to/object.txt"); -``` - -#### Deleting Files - -To delete an object from a bucket: - -```typescript -async function deleteFile(bucketName: string, filePath: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - await myBucket.fastRemove({ path: filePath }); - console.log(`File at ${filePath} deleted from ${bucketName}.`); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } -} - -// Use the function -deleteFile("exampleBucket", "path/to/object.txt"); +downloadFile("myBucket", "example.txt"); ``` #### Streaming Files -SmartBucket allows you to work with file streams, which can be useful for handling large files. - -To read a file as a stream: +For large files, use streams: ```typescript -import { ReplaySubject } from '@push.rocks/smartrx'; - -async function readFileStream(bucketName: string, filePath: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - const fileStream: ReplaySubject = await myBucket.fastGetStream({ path: filePath }); - fileStream.subscribe({ - next(chunk: Buffer) { - console.log("Chunk received:", chunk.toString()); - }, - complete() { - console.log("File read completed."); - }, - error(err) { - console.error("Error reading file stream:", err); - } - }); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } +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.")); } -// Use the function -readFileStream("exampleBucket", "path/to/object.txt"); +streamFile("myBucket", "largefile.txt"); ``` -To write a file as a stream: +#### Deleting Files + +Remove files with the `fastRemove` method: ```typescript -import { Readable } from 'stream'; - -async function writeFileStream(bucketName: string, filePath: string, readableStream: Readable) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - await myBucket.fastPutStream({ path: filePath, dataStream: readableStream }); - console.log(`File streamed to ${bucketName} at ${filePath}`); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } +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}.`); } -// Create a readable stream from a string -const readable = new Readable(); -readable.push('Hello world streamed as a file!'); -readable.push(null); // End of stream - -// Use the function -writeFileStream("exampleBucket", "path/to/streamedObject.txt", readable); +deleteFile("myBucket", "example.txt"); ``` -### Working with Directories +### Directory Operations -`@push.rocks/smartbucket` offers abstractions for directories within buckets for easier object management. You can create, list, and delete directories using the `Directory` class. +#### Listing Directories and Files -To list the contents of a directory: +You can navigate and list files in directories within a bucket: ```typescript -async function listDirectoryContents(bucketName: string, directoryPath: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - const baseDirectory: Directory = await myBucket.getBaseDirectory(); - const targetDirectory: Directory = await baseDirectory.getSubDirectoryByName(directoryPath); - - console.log('Listing directories:'); - const directories = await targetDirectory.listDirectories(); - directories.forEach(dir => { - console.log(`- ${dir.name}`); - }); +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('Listing files:'); - const files = await targetDirectory.listFiles(); - files.forEach(file => { - console.log(`- ${file.name}`); - }); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } + console.log('Directories:'); + (await targetDirectory.listDirectories()).forEach(dir => console.log(dir.name)); + + console.log('Files:'); + (await targetDirectory.listFiles()).forEach(file => console.log(file.name)); } -// Use the function -listDirectoryContents("exampleBucket", "some/directory/path"); +listDirectory("myBucket", "path/to/directory"); ``` -To create a file within a directory: +#### Managing Files in Directories + +Upload, download, and manage files using directory abstractions: ```typescript -async function createFileInDirectory(bucketName: string, directoryPath: string, fileName: string, fileContent: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - const baseDirectory: Directory = await myBucket.getBaseDirectory(); - const targetDirectory: Directory = await baseDirectory.getSubDirectoryByName(directoryPath); - await targetDirectory.createEmptyFile(fileName); // Create an empty file - const file = new File({ directoryRefArg: targetDirectory, fileName }); - await file.updateWithContents({ contents: fileContent }); - console.log(`File created: ${fileName}`); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } +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()}`); } -// Use the function -createFileInDirectory("exampleBucket", "some/directory", "newfile.txt", "Hello, world!"); +manageFilesInDirectory("myBucket", "myDir", "example.txt", "File content here"); ``` ### Advanced Features #### Bucket Policies -Manage bucket policies to control access permissions. This feature depends on the policies provided by the storage service (e.g., AWS S3, MinIO). +SmartBucket facilitates bucket policy management, depending on the cloud SDK's capabilities. -#### Object Metadata +#### Metadata Management -Retrieve and modify object metadata. Metadata can be useful for storing additional information about an object. - -To retrieve metadata: +You can retrieve and manipulate object metadata, employing it for additional data storage: ```typescript -async function getObjectMetadata(bucketName: string, filePath: string) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - const metadata = await mySmartBucket.minioClient.statObject(bucketName, filePath); - console.log("Object metadata:", metadata); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } +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); } -// Use the function -getObjectMetadata("exampleBucket", "path/to/object.txt"); +handleMetadata("myBucket", "example.txt"); ``` -To update metadata: +#### File Locking + +Lock files to prevent changes: ```typescript -async function updateObjectMetadata(bucketName: string, filePath: string, newMetadata: { [key: string]: string }) { - const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); - if (myBucket) { - await myBucket.copyObject({ - objectKey: filePath, - nativeMetadata: newMetadata, - deleteExistingNativeMetadata: false, - }); - console.log(`Metadata updated for ${filePath}`); - } else { - console.error(`Bucket ${bucketName} does not exist.`); - } +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.`); } -// Use the function -updateObjectMetadata("exampleBucket", "path/to/object.txt", { - customKey: "customValue" -}); +lockFile("myBucket", "example.txt"); ``` -#### Cloud Agnostic +### Cloud Agnosticism -`@push.rocks/smartbucket` is designed to work with multiple cloud providers, allowing for easier migration or multi-cloud strategies. This means you can switch from one provider to another with minimal changes to your codebase. +`@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. -Remember, each cloud provider has specific features and limitations. `@push.rocks/smartbucket` aims to abstract common functionalities, but always refer to the specific cloud provider's documentation for advanced features or limitations. +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 guide covers the basic to advanced scenarios of using `@push.rocks/smartbucket`. For further details, refer to the API documentation and examples. +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 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f3325ad..6be292d 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbucket', - version: '3.3.1', - description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.' + version: '3.3.2', + description: 'A TypeScript library facilitating cloud-agnostic object storage with capabilities such as bucket management, file operations, directory management, and advanced data streaming functionalities.' }