skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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);
    })();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search