3.5.1 • Published 6 months ago

bosscloudmediav2 v3.5.1

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

BossCloudMedia

BossCloudMedia is a powerful Node.js package for cloud file operations. It provides an easy-to-use interface for uploading, deleting, and listing files in cloud storage.

Installation

Install the package using npm:

npm install bosscloudmediav2

Generating an API Key

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

  1. Compose an email to contact@bmsinfoglobal.com
  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:

import BossCloudMedia from 'bosscloudmediav2';

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 options = {
      publicId: 'unique_file_identifier',
      quality: 80 // optional, defaults to 100
    };
    const result = await cloudMedia.upload(filePath, options);
    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 options = {
      publicId: 'unique_file_identifier',
      quality: 90 // optional
    };
    const result = await cloudMedia.upload(imageUrl, options);
    console.log('Upload from URL successful:', result);
  } catch (error) {
    console.error('Upload from URL failed:', error);
  }
}

uploadFromURL();

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('invalid_path.jpg', { publicId: 'test' });
  } 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(file, options)

Uploads a single file to the cloud storage.

Parameters:

  • file (string): The file path or URL to upload
  • options (Object): Upload options
    • publicId (string): A unique identifier for the file (required)
    • quality (number, optional): Image quality from 1 to 100 (default: 100)

Returns:

  • A Promise that resolves with the URL of the uploaded file.

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 FileInfo 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
    • size (string): The size of the file

License

This project is licensed under the MIT License.

3.5.1

6 months ago

3.5.0

6 months ago

3.4.8

6 months ago

3.4.7

6 months ago

3.4.6

6 months ago

3.4.9

6 months ago

3.4.5

7 months ago

3.4.4

8 months ago

3.4.0

8 months ago

3.4.3

8 months ago

3.4.2

8 months ago

3.4.1

8 months ago

3.3.0

10 months ago

3.2.0

10 months ago

3.1.0

11 months ago

3.0.0

11 months ago

2.7.0

11 months ago

2.6.0

11 months ago

2.5.0

11 months ago

2.4.0

11 months ago

2.3.0

11 months ago

2.2.0

11 months ago

2.1.7

11 months ago

2.1.6

11 months ago

2.1.5

11 months ago

2.1.4

11 months ago

2.1.3

11 months ago

2.1.2

11 months ago

2.1.1

11 months ago

2.1.0

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago