smarts3/readme.md
2024-04-14 18:16:08 +02:00

115 lines
5.0 KiB
Markdown

# @push.rocks/smarts3
create an s3 endpoint that maps to a local directory
## Install
To use @push.rocks/smarts3 in your project, you'll need to install it via npm. You can do so by running the following command in your project's root directory:
```sh
npm install @push.rocks/smarts3 --save
```
This will add `@push.rocks/smarts3` as a dependency to your project's `package.json` file and download the package into the `node_modules` directory.
## Usage
@push.rocks/smarts3 is designed to make it easy for developers to create an S3 compatible endpoint that maps to a local directory. This can be particularly useful for local development and testing, allowing you to mimic S3's functionality without the need for an actual S3 bucket.
Below, we'll go through how to get started, set up an S3 server, manage buckets, and perform common operations.
### Setting up the S3 Server
First, let's see how to set up and start an S3 server instance.
```typescript
import { Smarts3 } from '@push.rocks/smarts3';
async function startServer() {
// Creating and starting the Smarts3 instance
const smarts3Instance = await Smarts3.createAndStart({
port: 3000, // optional, defaults to 3000
cleanSlate: true // optional, if set to true, it will empty the directory on start
});
console.log('S3 server is up and running...');
return smarts3Instance;
}
startServer().catch(console.error);
```
### Creating a Bucket
With the server up and running, you can now create buckets.
```typescript
async function createBucket(smarts3Instance: Smarts3) {
// Creating a new bucket
const bucket = await smarts3Instance.createBucket('my-awesome-bucket');
console.log(`Bucket created: ${bucket.name}`);
}
// Assuming startServer() has been called and smarts3Instance has been received
createBucket(smarts3Instance).catch(console.error);
```
### Accessing Buckets and Uploading Files
To perform actions like uploading files, you will need to work with the underlying `SmartBucket` module. The `SmartBucket` module is part of the @push.rocks ecosystem and integrated into smarts3 for easy bucket and file management.
```typescript
import { SmartBucket, Bucket } from '@pushrocks/smartbucket';
async function uploadFile(smarts3Instance: Smarts3) {
// Getting the S3 descriptor to configure SmartBucket
const s3Descriptor = await smarts3Instance.getS3Descriptor();
const smartbucketInstance = new SmartBucket(s3Descriptor);
const bucket: Bucket = await smartbucketInstance.getBucket('my-awesome-bucket');
// Now let's upload a file to the bucket
const baseDirectory = await bucket.getBaseDirectory();
await baseDirectory.fastStore('hello.txt', 'Hello, world!');
console.log('File uploaded successfully.');
}
uploadFile(smarts3Instance).catch(console.error);
```
### Stopping the Server
Finally, when you are done, you can stop the Smarts3 server as follows:
```typescript
async function stopServer(smarts3Instance: Smarts3) {
await smarts3Instance.stop();
console.log('S3 server has been stopped.');
}
stopServer(smarts3Instance).catch(console.error);
```
### Complete Integration
Integrating @push.rocks/smarts3 into your development workflow offers a seamless way to mimic Amazon S3 locally. By creating an S3-compatible endpoint that maps to a local directory, you can develop and test applications that interact with S3 without incurring any cost or requiring internet connectivity. Thanks to its API, @push.rocks/smarts3 makes it easy to programmatically manage your local S3 server, create and delete buckets, and upload or download files, ensuring that your applications can be designed with cloud scalability in mind, right from the start.
## 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.