hdou533 / image-hosting

0 stars 0 forks source link

S3 images displaying #4

Closed hdou533 closed 1 month ago

hdou533 commented 1 month ago

S3 image links not displaying images but downloading it

have tried:

  1. set ContentType when uploading - not working
const uploadParams: UploadParams = {
    Bucket: bucket,
    Body: fileStream,
    Key: file.filename,
    ContentType: file.mimetype,
  };
hdou533 commented 1 month ago

Fix

  1. add ContentType: file.mimetype to uploadParams
  2. change storage of multer from diskStroage to const storage = multer.memoryStorage(); and change Key to const key =${Date.now()}_${file.originalname}; from s3 uploading file
  3. S3 policy changes

Bucket Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::hdou533-image-hosting/*"
        }
    ]
}

CORS

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "POST",
            "PUT"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]