0.0.14 • Published 3 years ago
@appstrax/storage v0.0.14
@appstrax/storage
This is a simple file storage storage service, which allows you to easily integrate appstrax storage into your web applications.
Getting Started
Create your Appstrax Storage API here: https://codecapsules.io/.
Installation
npm install @appstrax/storage --saveSetup
Initialize the storage library:
// import the storage library
import { storage } from '@appstrax/storage';
// initialize the storage service
const apiUrl = 'YOUR API URL HERE'; // eg. appstrax-storage-api-snidtu.codecapsules.co.za
storage.init(apiUrl);Upload Files:
Add the following file input and button in your html view:
<input
id="fileInput"
type="file"
style="display: none"
onchange="onFileSelected(this)"
/>
<button onclick="fileInput.click()">Upload file</button>Create a function called onFileSelected your the corresponding javascript
import { storage } from '@appstrax/storage';
async function onFileSelected(input) {
if (input.files && input.files.length) {
const file = input.files[0];
const filePath = 'images/';
try {
const result = await storage.uploadFile(file, filePath);
// use the downloadUrl to access your file.
console.log(result.downloadUrl);
/* Eg.
<img id="image" />
...
document.getElementById('image').src = result.downloadUrl;
*/
input.value = '';
} catch (err: any) {
// handle error
}
}
}If the file uploaded is stream-able IE a video or audio file. Add &stream=true to the file's downloadUrl. This will result in a better experience when using <video> and <audio> tags.
To force the file to download when the link is opened in a browser, add &download=true to the file's downloadUrl.
Delete An Uploaded File:
import { storage } from '@appstrax/storage';
try {
const downloadUrl = 'YOUR DOWNLOAD URL HERE';
await storage.deleteFile(downloadUrl);
} catch (err) {
// handle error
}