I am uploading files to my minio storage using nestjs-minio-client.
I have my application running on a docker container behind Nginx as a reverse proxy.
The minio storage is on another docker container on another server.
The uploading process works fine and the file is uploaded to the storage correctly. But the problem is that after a while, no more files can be uploaded unless I restart the container.
This is due to socket hang up:
[Nest] 20 - 11/29/2020, 12:36:57 PM [ExceptionsHandler] socket hang up +899287ms
Error: socket hang up
at connResetException (internal/errors.js:609:14)
at TLSSocket.socketOnEnd (_http_client.js:459:23)
at TLSSocket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1223:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Here comes my implementation:
I have imported the minioModule in my module:
import { MinioModule } from 'nestjs-minio-client';
@Module({
imports: [ MinioModule.register({
endPoint: minioConstants.MINIO_ENDPOINT,
useSSL: true,
accessKey: minioConstants.MINIO_ACCESSKEY,
secretKey: minioConstants.MINIO_SECRETKEY,})],
controllers: [...],
providers: [...],
exports: [...]
This is how I pass the file in my controller (using multer):
@Put('/avatar')
@UseInterceptors(FileInterceptor('avatar'))
async uploadAvatar(
@UploadedFile() avatarFile: IMulterFile
){
return {data: await this.uploadService.uploadImage(avatarFile)}
}
And here comes the uploadService:
import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common';
import { MinioService } from 'nestjs-minio-client';
import * as crypto from 'crypto'
import { IMulterFile } from '@shared/interfaces/file.interface';
@Injectable()
export class UploadService {
private readonly baseBucket = 'users' //fix ==> read from env
public get client() {
return this.minio.client;
}
constructor(private readonly minio: MinioService) {}
public async uploadImage(file: IMulterFile, baseBucket: string = this.baseBucket) {
if(!(file.mimetype.includes('image/jpeg') || file.mimetype.includes('image/png'))) {
throw new HttpException('Error uploading file', HttpStatus.BAD_REQUEST)
}
let temp_filename = Date.now().toString()
let hashedFileName = crypto.createHash('md5').update(temp_filename).digest("hex");
let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
const metaData = {
'Content-Type': file.mimetype,
'X-Amz-Meta-Testing': 1234,
};
let filename = hashedFileName + ext
const fileName: string = `${filename}`;
const fileBuffer = file.buffer;
await this.client.putObject(baseBucket,fileName,fileBuffer,metaData)
return {filename}
}
async delete(objetName: string, baseBucket: string = this.baseBucket) {
await this.client.removeObject(baseBucket, objetName);
}
}
I have also tried nestjs-minio as the client, but the same issue occurs after a while that the container is used.
Are there any ideas about why this is happening and how to fix it?