Storage

Upload and download

Effortlessly upload and download files with Nuvix Storage. Learn how to handle file uploads, manage file versions, and ensure secure downloads in your applications.

You can upload and download files both programmatically using SDKs or through the Nuvix Console.

Create file

After you create a bucket or have navigated to bucket details, you can access the Files tab so you can upload, view, delete and update files in the bucket using the Nuvix project's dashboard. You can also perform all those operations from Nuvix's client SDK, server SDKs, and REST APIs as long as you have the proper permission.

When you are in the Files tab, you can click Add File and select a file to upload. If the bucket is configured to accept the file type and size you are uploading, your file will be uploaded, and you will see the file in the list of files.

You can also upload files programmatically using our SDKs:

import { Client, ID } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>');


// Upload file from file input
const file = document.getElementById('uploader').files[0];
const response = await nx.storage.createFile({
    bucketId: '<BUCKET_ID>',
    fileId: ID.unique(),
    file: file
});

console.log(response); // Success
import { Client, ID, InputFile } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>')
    .setKey('<API_KEY>');


// Upload from file path
const file = InputFile.fromPath('/path/to/file.jpg', 'file.jpg');
const response = await nx.storage.createFile({
    bucketId: '<BUCKET_ID>',
    fileId: ID.unique(),
    file: file
});

console.log(response); // Success

Large files

When you are trying to upload any files above 5MB, you will need to upload them in chunks for better reliability and performance. If you're using a Nuvix SDK, this is handled automatically. If you're not using an SDK, you can learn more about REST API file handling in the API documentation.

InputFile

Every language and platform handles file inputs differently. This section shows the expected input type of each SDK. Where applicable, Nuvix provides an InputFile class to accept multiple file sources, like paths, buffers, or plain text.

Client SDKs

The Nuvix Web SDK expects a File object for file creation. This is most commonly associated with DOM file inputs.

For example, for the input tag <input type="file" id="uploader">, you would call create file like this:

const response = await nx.storage.createFile({
    bucketId: '<BUCKET_ID>',
    fileId: ID.unique(),
    file: document.getElementById('uploader').files[0]
});

The Nuvix React Native SDK expects a file object with the following properties for file inputs:

PropertyDescription
nameThe name of the file.
typeThe MIME type of the file.
sizeThe size of the file in bytes.
uriThe URI of the file on the device.

This object structure aligns with what is typically returned from image picker libraries such as react-native-image-picker:

// Example with react-native-image-picker
import { launchImageLibrary } from 'react-native-image-picker';

const pickImage = async () => {
  const result = await launchImageLibrary({
    mediaType: 'photo',
  });

  if (result.assets && result.assets[0]) {
    const fileInfo = result.assets[0];

    return {
      name: fileInfo.fileName,
      type: fileInfo.type,
      size: fileInfo.fileSize,
      uri: fileInfo.uri,
    };
  }
};

Server SDKs

In browser environments, you can use the File object directly. For Node.js environments, import the InputFile class from 'nuvix/file'.

When using InputFile, the following methods are available:

MethodDescription
InputFile.fromPath(filePath, filename)Used to upload files from a provided path.
InputFile.fromBuffer(buffer, filename)Used to upload files from a Buffer or Blob object.
InputFile.fromPlainText(content, filename)Used to upload files in plain text. Expects a string encoded in UTF-8.

The Nuvix Deno SDK expects an InputFile class for file inputs.

MethodDescription
InputFile.fromPath(filePath, filename)Used to upload files from a provided path.
InputFile.fromBuffer(buffer, filename)Used to upload files from a Uint8Array object.
InputFile.fromPlainText(content, filename)Used to upload files in plain text. Expects a string encoded in UTF-8.

Get file

To get metadata about a file, use the getFile method.

import { Client } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>');


const file = await nx.storage.getFile({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>'
});

console.log(file); // File metadata

Download file

To download a file, use the getFileDownload method.

import { Client } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>');


// Download file data
const fileData = await nx.storage.getFileDownload({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>'
});

console.log(fileData); // File data as ArrayBuffer

// Get download URL without downloading
const downloadUrl = nx.storage.getFileDownloadURL({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>'
});

console.log(downloadUrl); // URL object

Get File Preview

To get a file preview image, use the getFilePreview method.

import { Client } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>');


// Get preview image data
const previewData = await nx.storage.getFilePreview({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>',
    width: 200,
    height: 200,
    quality: 90
});

console.log(previewData); // Image data as ArrayBuffer

// Get preview URL without downloading
const previewUrl = nx.storage.getFilePreviewURL({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>',
    width: 200,
    height: 200,
    quality: 90
});

console.log(previewUrl); // URL object

View File

To view a file, use the getFileView method.

import { Client } from "@nuvix/client";

const nx = new Client()
    .setEndpoint('https://api.nuvix.in/v1')
    .setProject('<PROJECT_ID>');


// Get file data
const fileData = await nx.storage.getFileView({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>'
});

console.log(fileData); // File data as ArrayBuffer

// Get view URL without downloading
const viewUrl = nx.storage.getFileViewURL({
    bucketId: '<BUCKET_ID>',
    fileId: '<FILE_ID>'
});

console.log(viewUrl); // URL object

How is this guide?

Last updated on

Upload and download