Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@push.rocks/smarts3

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@push.rocks/smarts3

A Node.js TypeScript package to create a local S3 endpoint for simulating AWS S3 operations using mapped local directories for development and testing purposes.

  • 2.2.5
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source
# @push.rocks/smarts3

A Node.js TypeScript package to create a local S3 endpoint for development and testing using mapped local directories, simulating AWS S3.

## Install

To integrate `@push.rocks/smarts3` with your project, you need to install it via npm. Execute the following command within your project's root directory:

```sh
npm install @push.rocks/smarts3 --save
```

This command will add @push.rocks/smarts3 as a dependency in your project's package.json file and download the package into the node_modules directory.

Usage

Overview

The @push.rocks/smarts3 module allows users to create a mock S3 endpoint that maps to a local directory using s3rver. This simulation of AWS S3 operations facilitates development and testing by enabling file uploads, bucket creation, and other interactions locally. This local setup is ideal for developers looking to test cloud file storage operations without requiring access to a real AWS S3 instance.

In this comprehensive guide, we will explore setting up a local S3 server, performing operations like creating buckets and uploading files, and how to effectively integrate this into your development workflow.

Setting Up the Environment

To begin any operations, your environment must be configured correctly. Here’s a simple setup procedure:

import * as path from 'path';
import { promises as fs } from 'fs';

async function setupEnvironment() {
  const packageDir = path.resolve();
  const nogitDir = path.join(packageDir, './.nogit');
  const bucketsDir = path.join(nogitDir, 'bucketsDir');

  try {
    await fs.mkdir(bucketsDir, { recursive: true });
  } catch (error) {
    console.error('Failed to create buckets directory!', error);
    throw error;
  }

  console.log('Environment setup complete.');
}

setupEnvironment().catch(console.error);

This script sets up a directory structure required for the smarts3 server, ensuring that the directories needed for bucket storage exist before starting the server.

Starting the S3 Server

Once your environment is set up, start an instance of the smarts3 server. This acts as your local mock S3 endpoint:

import { Smarts3 } from '@push.rocks/smarts3';

async function startServer() {
  const smarts3Instance = await Smarts3.createAndStart({
    port: 3000,
    cleanSlate: true,
  });

  console.log('S3 server is up and running at http://localhost:3000');
  return smarts3Instance;
}

startServer().catch(console.error);

Parameters:

  • Port: Specify the port for the local S3 server. Defaults to 3000.
  • CleanSlate: If true, clears the storage directory each time the server starts, providing a fresh test state.

Creating and Managing Buckets

With your server running, create buckets for storing files. A bucket in S3 acts similarly to a root directory.

async function createBucket(smarts3Instance: Smarts3, bucketName: string) {
  const bucket = await smarts3Instance.createBucket(bucketName);
  console.log(`Bucket created: ${bucket.name}`);
}

startServer()
  .then((smarts3Instance) => createBucket(smarts3Instance, 'my-awesome-bucket'))
  .catch(console.error);

Uploading and Managing Files

Uploading files to a bucket uses the SmartBucket module, part of the @push.rocks/smartbucket ecosystem:

import { SmartBucket } from '@push.rocks/smartbucket';

async function uploadFile(
  smarts3Instance: Smarts3,
  bucketName: string,
  filePath: string,
  fileContent: string,
) {
  const s3Descriptor = await smarts3Instance.getS3Descriptor();
  const smartbucketInstance = new SmartBucket(s3Descriptor);
  const bucket = await smartbucketInstance.getBucket(bucketName);

  await bucket.getBaseDirectory().fastStore(filePath, fileContent);
  console.log(`File "${filePath}" uploaded successfully to bucket "${bucketName}".`);
}

startServer()
  .then(async (smarts3Instance) => {
    await createBucket(smarts3Instance, 'my-awesome-bucket');
    await uploadFile(smarts3Instance, 'my-awesome-bucket', 'hello.txt', 'Hello, world!');
  })
  .catch(console.error);

Listing Files in a Bucket

Listing files within a bucket allows you to manage its contents conveniently:

async function listFiles(smarts3Instance: Smarts3, bucketName: string) {
  const s3Descriptor = await smarts3Instance.getS3Descriptor();
  const smartbucketInstance = new SmartBucket(s3Descriptor);
  const bucket = await smartbucketInstance.getBucket(bucketName);

  const baseDirectory = await bucket.getBaseDirectory();
  const files = await baseDirectory.listFiles();

  console.log(`Files in bucket "${bucketName}":`, files);
}

startServer()
  .then(async (smarts3Instance) => {
    await createBucket(smarts3Instance, 'my-awesome-bucket');
    await listFiles(smarts3Instance, 'my-awesome-bucket');
  })
  .catch(console.error);

Deleting a File

Managing storage efficiently involves deleting files when necessary:

async function deleteFile(smarts3Instance: Smarts3, bucketName: string, filePath: string) {
  const s3Descriptor = await smarts3Instance.getS3Descriptor();
  const smartbucketInstance = new SmartBucket(s3Descriptor);
  const bucket = await smartbucketInstance.getBucket(bucketName);

  await bucket.getBaseDirectory().fastDelete(filePath);
  console.log(`File "${filePath}" deleted from bucket "${bucketName}".`);
}

startServer()
  .then(async (smarts3Instance) => {
    await createBucket(smarts3Instance, 'my-awesome-bucket');
    await deleteFile(smarts3Instance, 'my-awesome-bucket', 'hello.txt');
  })
  .catch(console.error);

Scenario Integrations

Development and Testing
  1. Feature Development: Use @push.rocks/smarts3 to simulate file upload endpoints, ensuring your application handles file operations correctly before going live.

  2. Continuous Integration/Continuous Deployment (CI/CD): Integrate with CI/CD pipelines to automatically test file interactions.

  3. Data Migration Testing: Simulate data migrations between buckets to perfect processes before implementation on actual S3.

  4. Onboarding New Developers: Offer new team members hands-on practice with mock setups to improve their understanding without real-world consequences.

Stopping the Server

Safely shutting down the server when tasks are complete ensures system resources are managed well:

async function stopServer(smarts3Instance: Smarts3) {
  await smarts3Instance.stop();
  console.log('S3 server has been stopped.');
}

startServer()
  .then(async (smarts3Instance) => {
    await createBucket(smarts3Instance, 'my-awesome-bucket');
    await stopServer(smarts3Instance);
  })
  .catch(console.error);

In this guide, we walked through setting up and fully utilizing the @push.rocks/smarts3 package for local development and testing. The package simulates AWS S3 operations, reducing dependency on remote services and allowing efficient development iteration cycles. By implementing the practices and scripts shared here, you can ensure a seamless and productive development experience using the local S3 simulation capabilities of @push.rocks/smarts3.


## 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.

Keywords

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc