0

In my Nextjs project I'm trying to copy files using (CopyObjectCommand).

Using @aws-sdk/client-s3: 3.616.0

The PutObjectCommand and DeleteObjectCommand are working fine. However, the CopyObjectCommand returns an error

Below is my code

// Copy S3 Object
export const copyS3Object = (sourceKey, destinationKey ) => {
    try {
      const bucket = process.env.AWS_S3_BUCKET;
      const encodedSource = encodeURIComponent(`${bucket}/${sourceKey}`);

      const params = {
        Bucket: bucket,
        CopySource: encodedSource,
        Key: destinationKey,
      };

      const command = new CopyObjectCommand(params);
      const data =  await s3Client.send(command);
      
      return { status: 'success' };
    } catch (error) {
      console.error("Error in copyS3Object:", error.message);
      console.error("Stack trace:", error.stack);
      throw new Error("Failed to copy S3 object");
    }
  },
});

How I call it

   const newKey = 
   courses/${courseId}/${variant.name}/${variant.filename};
   const oldKey = `courses/temp/${variant.name}/${variant.filename};
   await copyS3Object({ sourceKey: oldKey, destinationKey: newKey });

Error : A header you provided implies functionality that is not implemented

The error

I have tried using an encoded and non encoded versions of the key and copySource. Also tried hard coding those values and still I get the error. I am using javascript and not Typescript on this project.

2 Answers 2

0

Can you try below code for calling with async :

(async () => {
  const newKey = `courses/${courseId}/${variant.name}/${variant.filename}`;
  const oldKey = `courses/temp/${variant.name}/${variant.filename}`;
  await copyS3Object(oldKey, newKey);
})();
Sign up to request clarification or add additional context in comments.

1 Comment

Still the same error
0

I was able to figure this out. Turns out my backend Convex was adding a Transform Encoding header when sending the request to aws. So I stopped using it and I am running it in a pure node.js backend and it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.