0
const S3 = require('aws-sdk/clients/s3');

const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");

dotenv.config();

const bucketName = process.env.AWS_BUCKET_NAME
const region = process.env.AWS_BUCKET_REGION
const accessKeyId = process.env.AWS_ACCESS_KEY
const secretAccessKey = process.env.AWS_SECRET_KEY

const s3 = new S3({
    region,
    accessKeyId,
    secretAccessKey
})

router.get("/:id", async (req, res) => {
    try {
        const post = await Post.findById(req.params.id);
        
        const getObjectParams = {
            Bucket: bucketName,
            Key: post.photo,
        }

        const command = new GetObjectCommand(getObjectParams);
        const url = await getSignedUrl(s3, command, { expiresIn: 3600 });
        post.imageUrl = url
        
        res.status(200).json(post);
        } catch (err) {
        console.error('errorrr', err);
        res.status(500).json(err);
    }
});

Here is my code I've console logged post, getObjectParams, command and everything is there but when I console log url it's not logging and when I console.log errorrr it logs Cannot read properties of undefined (reading 'clone')

What is the issue here?

I think issue is with function getSignedUrl, but not sure what it is

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • You should learn basic debugging skills. Pinpoint the place where you get the issue. Most basic would be to print output of specific commands, like `console.log(command)`. But to be effective look into connecting debugger to whatever IDE you're using – karjan Feb 19 '23 at 09:47

0 Answers0