aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.96k stars 557 forks source link

get the response header of PutObjectCommand #6089

Closed ZenWayne closed 1 month ago

ZenWayne commented 1 month ago

Describe the feature

I want to retrieve some value located on response header of PutObjectCommand, however I can not find a way to do that, here are my code snippet

      const S3 = new S3Client({...})
      await S3.send(new PutObjectCommand(params), (err, data) => {
        //cannot get the response header value of the key 'X-Amz-Meta-Cid'  in this function body
        console.log('saveDataToIPFS response header:', data);
        if (err) {
          console.error(err);
        } else {
          console.log('saveDataToIPFS response:', data.httpResponse.headers);
        }
      });

Use Case

I use aws-sdk for filebase ipfs platform, when I use PutObjectCommand I wanna get CID of the file I uploaded. and the CID is located on http response header, whose name is 'X-Amz-Meta-Cid'

Proposed Solution

I preferred add a variable referred to the response header in the return value of S3.send() function

Other Information

No response

Acknowledgements

SDK version used

@aws-sdk/client-s3 v3.574.0

Environment details (OS name and version, etc.)

Windows 11

ZenWayne commented 1 month ago

checking the cors setting and use middlewarestack.add this problem has been solved. add x-amz-meta-cid on cors setting

{
    "CORSRules": [
        {
            "AllowedHeaders": [
                "*"
            ],
            "AllowedMethods": [
                "GET",
                "PUT",
                "POST",
                "DELETE"
            ],
            "AllowedOrigins": [
              ""
            ],
            "ExposeHeaders": [
                "x-amz-server-side-encryption",
                "x-amz-request-id",
                "x-amz-id-2",
                "x-amz-meta-cid"
            ],
            "MaxAgeSeconds": 3000
        }
    ]
}

fetch CID on javascript from aws v3 sdk example

const command = new PutObjectCommand({...});

command.middlewareStack.add(
  (next) => async (args) => {
    // Check if request is incoming as middleware works both ways
    const response = await next(args);
    if (!response.response.statusCode) return response;

    // Get cid from headers
    const cid = response.response.headers["x-amz-meta-cid"];
    console.log(cid);
    return response;
  },
  {
    step: "build",
    name: "addCidToOutput",
  },
);

const res = await client.send(command);
github-actions[bot] commented 1 month ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.