0

After uploading an image to S3 bucket, it looks strange most of the time, but the same images looks OK sometimes, so I'm not sure what's going on. It started happening from last week and before that it was working fine.

Here is a sample image that I uploaded, and after uploading, it appears as follows. https://hub-uploads-stage.s3.us-east-2.amazonaws.com/gallery-documents/file-1675056885128-alphabet-gcf3617eaf_1920.jpg

Original Image https://i.stack.imgur.com/WgmVh.jpg

I'm using packages: multer, multer-s3 with node js 16.19.0

I'm using following dependencies

multer: ^1.4.2

multer-s3: ^2.10.0

aws-sdk: ^2.1002.0

file-type: ^16.5.3

Here is the code I used to upload files.

import AWS from "aws-sdk";
import multer from "multer";
import multerS3 from "multer-s3";
import { fromBuffer } from 'file-type';
import stream from 'stream';

AWS.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION,
    signatureVersion: "s3v4",
});

const s3 = new AWS.S3();
export const basePath = {
    profile: "profile-images",
    post: "post-documents",
    slider: "slider-images",
    gallery: "gallery-documents",
    icons: "icons",
    posters: "product-posters",
    chat: "chat"
}
const buckets = {
    uploads: process.env.S3_BUCKET_UPLOADS || 'hub-uploads-stage'
}
const mimeTypes = {
    image: [
        "image/gif",
        "image/jpg",
        "image/jpeg",
        "image/png",
        "image/tiff",
        "image/pipeg",
        "image/svg+xml",
        "image/bmp",
        "image/x-xbitmap",
        "image/x-icon"
    ]
}


const storage = (Bucket: any, uploadBasePath: any) => multerS3({
    s3,
    bucket: Bucket,
    acl: 'public-read',
    contentType: function (req: any, file: any, callback: any) {
        file.stream.once('data', async function (firstChunk: any) {
            var type = await fromBuffer(firstChunk)
            var mime = (type === null ? 'application/octet-stream' : type?.mime)
            var outStream = new stream.PassThrough()
            outStream.write(firstChunk)
            file.stream.pipe(outStream)
            callback(null, mime, outStream)
        })
    },
    metadata: function (req: any, file: any, callback: any) {
        callback(null, { fieldName: file.originalname })
    },
    key: function (req: any, file: any, callback: any) {
        let fileName = `${file.fieldname}-${Date.now()}-${file.originalname}`;
        let filePath = uploadBasePath + '/' + fileName;
        callback(null, filePath)
    }
})


export const uploader = {
    profile: multer({
        storage: storage(buckets.uploads, basePath.profile),
        fileFilter: function (req, file, callback) {
            if (mimeTypes.image.includes(file.mimetype)) {
                callback(null, true)
            } else {
                callback(null, false)
            }
        }
    }),
    post: multer({
        storage: storage(buckets.uploads, basePath.post)
    }),
    slide: multer({
        storage: storage(buckets.uploads, basePath.slider),
        fileFilter: function (req, file, callback) {
            if (mimeTypes.image.includes(file.mimetype)) {
                callback(null, true)
            } else {
                callback(null, false)
            }
        }
    }),
    gallery: multer({
        storage: storage(buckets.uploads, basePath.gallery),
        fileFilter: function (req, file, callback) {
            console.log("File in filter =>", file)


                callback(null, true)
            
        }
    }),
    icon: multer({
        storage: storage(buckets.uploads, basePath.icons)
    }),
    productPoster: multer({
        storage: storage(buckets.uploads, basePath.posters),
        fileFilter: function (req, file, callback) {
            if (mimeTypes.image.includes(file.mimetype)) {
                callback(null, true)
            } else {
                callback(null, false)
            }
        }
    }),
    chat: multer({
        storage: storage(buckets.uploads, basePath.chat)
    })
};

Is there something i need to upgrade or change? Thanks

Dargha Patel
  • 35
  • 1
  • 4

0 Answers0