Api for upload file in blob storage in nestjs

Viewed 202

I am trying to upload the file in blob storage using @azure/storage-blob and @types/multer npm package.

service.ts

getblob(filename: any): BlockBlobClient {
    const connectionString = process.env.CONNECTION_STRING,
        containerName = process.env.BLOB_CONTAINER;

    const blobServiceClient =
        BlobServiceClient.fromConnectionString(connectionString);
    const blobContainer =
        blobServiceClient.getContainerClient(containerName);
    return blobContainer.getBlockBlobClient(filename);
} 


 async FileUpload(file: Express.Multer.File) {
    try {
        const blockblob = this.getblob(file.originalname);
        console.log(file.buffer);
        return await blockblob.uploadData(file.buffer);
    } catch (err) {
        console.log(err);
    }
}

controller.ts

 @Post("uploadfile")
@UseInterceptors(FileInterceptor("Server"))
async uploadFile(@UploadedFile() file: Express.Multer.File) {
    try {
        return file;
    } catch (err) {
        console.log(err);
    }
}

But I am getting this error.

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined

anyone has any solution for this error. am I doing something wrong?

1 Answers
This error was expecting an instance of Buffer so this way we can do:-

server.ts
 
async FileUpload(file: Express.Multer.File) {
try {
    const blockblob = this.getblob(file.originalname);
    console.log(file.buffer);
const blob = Buffer.from(file.buffer)
 return await blockblob.uploadData(file.buffer);
} catch (err) {
 console.log(err);
 }}
Related