Skip to main content

Storage Service

The module exports a storage service under the StorageService injection token.

The underlying implementation depends on the selected provider (S3 or MinIO), but the public API is consistent.

Methods

MethodDescription
uploadAsync(args)Upload a file from a Buffer.
uploadWithBase64Async(args)Upload a file from a Base64 string (e.g. data:<mime>;base64,...).
uploadWithUrlAsync(args)Download a file from a URL and upload it.
deleteAsync(path)Delete an object by path/key.
createShareLinkAsync(path, expiresIn?)Create a share link (e.g. pre-signed URL) for an object.

Upload result

All upload methods return a common result shape.

FieldDescription
pathFull object path (including optional folder prefix).
fileNameFile name with extension.
sizeFile size in bytes.
extensionDetected (or provided) extension.
mimeDetected (or provided) mime type.

Provider-specific fields may be added depending on the provider.

Usage examples

Injecting the service

StorageService is exported using the StorageService token.

files.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Injectable()
export class FilesService {
constructor(
@Inject('StorageService')
private readonly storageService: NvsStorageService,
) {}
}

uploadAsync(args) (Buffer)

Example with @nestjs/platform-express + Multer (@UploadedFile()):

files.controller.ts
import {
Controller,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Inject } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Controller('files')
export class FilesController {
constructor(@Inject('StorageService') private readonly storage: NvsStorageService) {}

@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async upload(@UploadedFile() file: Express.Multer.File) {
return this.storage.uploadAsync({
file: file.buffer,
fileName: file.originalname.replace(/\.[^/.]+$/, ''),
path: 'uploads',
maxSize: 10 * 1024 * 1024,
validateFileTypes: ['image/png', 'image/jpeg'],
});
}
}

uploadWithBase64Async(args) (Base64)

files.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Injectable()
export class FilesService {
constructor(@Inject('StorageService') private readonly storage: NvsStorageService) {}

async uploadAvatarBase64(base64: string) {
return this.storage.uploadWithBase64Async({
file: base64,
fileName: 'avatar',
path: 'avatars',
// optional fallback when file-type detection fails
defaultMime: { mime: 'image/png', extension: 'png' },
});
}
}

uploadWithUrlAsync(args) (URL)

files.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Injectable()
export class FilesService {
constructor(@Inject('StorageService') private readonly storage: NvsStorageService) {}

async importFromUrl(url: string) {
return this.storage.uploadWithUrlAsync({
file: url,
fileName: 'imported-file',
path: 'imports',
maxSize: 20 * 1024 * 1024,
});
}
}

deleteAsync(path)

files.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Injectable()
export class FilesService {
constructor(@Inject('StorageService') private readonly storage: NvsStorageService) {}

async remove(path: string) {
await this.storage.deleteAsync(path);
return { deleted: true };
}
}

createShareLinkAsync(path, expiresIn?)

files.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { NvsStorageService } from 'nestjs-nvs-storage';

@Injectable()
export class FilesService {
constructor(@Inject('StorageService') private readonly storage: NvsStorageService) {}

async share(path: string) {
const url = await this.storage.createShareLinkAsync(path, 60 * 10); // 10 minutes
return { url };
}
}

Notes

  • File type is detected via file-type. If detection fails, you can provide defaultMime.
  • If maxSize is provided, uploads larger than the limit will throw.
  • If validateFileTypes is provided, uploads with a mime type not in the allowlist will throw.
  • When using uploadWithUrlAsync, the module downloads the remote file via HttpService before uploading.