A TypeScript package for uploading single or multiple files to the Media Cloud API, fetching uploaded media, soft delete and permanent delete.
- jpeg
- png
- jpg
- gif
- svg
- mp4
- mp3
- doc
- docx
- xls
- xlsx
- ppt
npm install mediacloud-server-client
import { MediaCloudUploader } from 'mediacloud-server-client';
async function main() {
const uploader = new MediaCloudUploader('your-api-key');
try {
// Upload a single file
const imageUrl = await uploader.uploadFile({
filePath: '/path/to/your/file1.jpg',
optimize: true //optional and it only works for image files, other file type will not be optimized
});
console.log('Uploaded image URL:', imageUrl);
// Upload multiple files
const multipleImageUrls = await uploader.uploadMultipleFiles({
filePaths: ['/path/to/your/file1.jpg', '/path/to/your/file2.png', '/path/to/your/file3.gif'],
optimize: true //optional and it will only optmized image files, other file type will not be optimized
});
console.log('Uploaded image URLs:', multipleImageUrls);
// Fetch uploaded media
const page = 1
const uploadedMedia = await uploader.getMedias(page);
console.log('Uploaded media:', uploadedMedia);
// Move item to trash
const mediaId = 'bWVkaWEvaW1hZ2VzL29yaWdpbmFsL3JqMExYTUlPOUtLSDc2UXRwZkRIeEJ6NGwzN1VIb01aVWdUbnR0cVcucG5n';
await uploader.softDeleteMedia(mediaIdToDelete);
console.log('Media deleted successfully');
// Delete a media item
const mediaIdToDelete = 'bWVkaWEvaW1hZ2VzL29yaWdpbmFsL3JqMExYTUlPOUtLSDc2UXRwZkRIeEJ6NGwzN1VIb01aVWdUbnR0cVcucG5n';
await uploader.deleteMedia(mediaIdToDelete);
console.log('Media deleted successfully');
} catch (error) {
console.error('Operation failed:', error);
}
}
main();
A class that handles file uploads to the Media Cloud API, fetches uploaded media, and deletes media.
To run the tests for this package, make sure you have Jest installed:
npm test
new MediaCloudUploader(apiKey: string)
apiKey
(string): Your Media Cloud API key.
Uploads a single file to the Media Cloud API.
filePath
(string): The path to the file you want to upload.optimize
(boolean, optional): Whether to optimize the uploaded media. Defaults totrue
.
A promise that resolves to the URL of the uploaded media.
Uploads multiple files to the Media Cloud API.
filePaths
(string[]): An array of file paths to upload.optimize
(boolean, optional): Whether to optimize the uploaded media. Defaults totrue
.
A promise that resolves to an array of URLs of the uploaded media.
Fetches the list of uploaded media from the Media Cloud API.
page
(number, optional): The page number of results to fetch. Defaults to 1.
A promise that resolves to a PaginatedMediaResponse
object containing the list of media URLs and pagination information.
Deletes a specific media item from the Media Cloud API.
mediaId
(string): The ID of the media item to delete. This is the last part of the media URL after/media/
.
A promise that resolves when the media item is successfully deleted.
A string representing the URL of an uploaded media item.
An object containing the paginated list of media items and related pagination information.
MIT