npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bosscloudmedia

v1.2.9

Published

A powerful Node.js package for cloud file operations using AWS S3

Downloads

70

Readme

BossCloudMedia

BossCloudMedia is a simple yet powerful Node.js package for cloud file operations using AWS S3. It provides an easy-to-use interface for uploading, deleting, and listing files in the cloud.

Installation

Install the package using npm:

npm install bosscloudmedia

Generating an API Key

To use BossCloudMedia, you'll need an API key. Follow these steps to obtain one:

  1. Compose an email to [email protected]
  2. Include the following information in your email:
    • Subject: "API Key Request for BossCloudMedia"
    • Body: Your company name
    • Any specific requirements or questions you may have

The BossCloudMedia team will review your request and provide you with an API key along with any additional instructions or information.

Usage

Initializing BossCloudMedia

Once you have your API key, import and initialize the BossCloudMedia class:

const BossCloudMedia = require('bosscloudmedia');

const cloudMedia = new BossCloudMedia({ api_key: 'your_api_key_here' });

Uploading a Single File

To upload a single file, use the upload method:

async function uploadSingleFile() {
  try {
    const filePath = '/path/to/your/file.jpg';
    const fileName = 'image.jpg';
    const fileBuffer = await fs.promises.readFile(filePath);
    
    const result = await cloudMedia.upload(fileBuffer, fileName);
    console.log('Upload successful:', result);
  } catch (error) {
    console.error('Upload failed:', error);
  }
}

uploadSingleFile();

You can also upload a file from a URL:

async function uploadFromURL() {
  try {
    const imageUrl = 'https://example.com/image.jpg';
    const fileName = 'image_from_url.jpg';
    
    const result = await cloudMedia.upload(imageUrl, fileName);
    console.log('Upload from URL successful:', result);
  } catch (error) {
    console.error('Upload from URL failed:', error);
  }
}

uploadFromURL();

Uploading Multiple Files

To upload multiple files, use the uploadMultiple method:

async function uploadMultipleFiles() {
  try {
    const files = [
      { buffer: await fs.promises.readFile('/path/to/file1.jpg'), name: 'file1.jpg' },
      { buffer: await fs.promises.readFile('/path/to/file2.png'), name: 'file2.png' },
      { url: 'https://example.com/image.jpg', name: 'file3.jpg' }
    ];
    
    const inputs = files.map(file => file.buffer || file.url);
    const fileNames = files.map(file => file.name);
    
    const results = await cloudMedia.uploadMultiple(inputs, fileNames);
    console.log('Multiple uploads results:', results);
  } catch (error) {
    console.error('Multiple uploads failed:', error);
  }
}

uploadMultipleFiles();

Deleting a Single File

To delete a single file, use the delete method:

async function deleteSingleFile() {
  try {
    const fileName = 'file_to_delete.jpg';
    const result = await cloudMedia.delete(fileName);
    console.log('Deletion successful:', result);
  } catch (error) {
    console.error('Deletion failed:', error);
  }
}

deleteSingleFile();

Deleting Multiple Files

To delete multiple files, pass an array of file names to the delete method:

async function deleteMultipleFiles() {
  try {
    const fileNames = ['file1.jpg', 'file2.png', 'file3.jpg'];
    const results = await cloudMedia.delete(fileNames);
    console.log('Multiple deletions results:', results);
  } catch (error) {
    console.error('Multiple deletions failed:', error);
  }
}

deleteMultipleFiles();

Listing All Files

To retrieve a list of all files in your cloud storage, use the listFiles method:

async function listAllFiles() {
  try {
    const files = await cloudMedia.listFiles();
    console.log('Files in storage:', files);
  } catch (error) {
    console.error('Failed to list files:', error);
  }
}

listAllFiles();

Error Handling

All methods may throw errors. It's important to use try-catch blocks to handle these errors gracefully:

async function errorHandlingExample() {
  try {
    // Trying to upload an invalid file
    await cloudMedia.upload(null, 'invalid.jpg');
  } catch (error) {
    console.error('Expected error:', error.message);
  }

  try {
    // Trying to delete with an invalid filename
    await cloudMedia.delete('');
  } catch (error) {
    console.error('Expected error:', error.message);
  }
}

errorHandlingExample();

API Reference

new BossCloudMedia(config)

Creates a new instance of BossCloudMedia.

Parameters:

  • config (Object): Configuration object
    • api_key (String): Your API key for authentication

cloudMedia.upload(input, fileName)

Uploads a single file to the cloud storage.

Parameters:

  • input (Buffer | string): The file buffer or URL to upload
  • fileName (string): The name to give the file in cloud storage

Returns:

  • A Promise that resolves with the upload response (string URL of the uploaded file).

cloudMedia.uploadMultiple(inputs, fileNames)

Uploads multiple files to the cloud storage.

Parameters:

  • inputs (Array<Buffer | string>): An array of file buffers or URLs to upload
  • fileNames (Array): An array of file names corresponding to the inputs

Returns:

  • A Promise that resolves with an array of upload results.

cloudMedia.delete(fileName)

Deletes a file or multiple files from the cloud storage.

Parameters:

  • fileName (string | Array): The name of the file to delete, or an array of file names to delete multiple files

Returns:

  • A Promise that resolves with the deletion response or an array of deletion responses.

cloudMedia.listFiles()

Retrieves a list of all files in the cloud storage.

Returns:

  • A Promise that resolves with an array of file objects. Each object contains:
    • name (string): The name of the file
    • url (string): The URL of the file
    • lastModified (number): The last modified timestamp in milliseconds (UTC)
    • size (string): The size of the file in a human-readable format (e.g., "1.5 MB")

License

This project is licensed under the MIT License.