Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 GetObject returns a stream body #6707

Closed
GeorgeBrn opened this issue Dec 2, 2024 · 7 comments
Closed

S3 GetObject returns a stream body #6707

GeorgeBrn opened this issue Dec 2, 2024 · 7 comments
Labels
documentation This is a problem with documentation. duplicate This issue is a duplicate.

Comments

@GeorgeBrn
Copy link

Describe the issue

This documentation is absolutely deplorable, without chatGPT I would've been unable to use S3. Literally not one word about 'getObjectCommand' returning data in streams and you having to handle the chunks with a promise. All I wanted to do was to upload an image from a nodeJs server and then read it from S3. Of course, this simple task took me several hours because getting the body from an S3 file is apparently a herculean task

Links

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/GetObjectCommand/

@GeorgeBrn GeorgeBrn added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Dec 2, 2024
@kuhe
Copy link
Contributor

kuhe commented Dec 2, 2024

The linked page states the return type of the body in S3 GetObject is a stream in the section "Example Syntax" and in the section "GetObjectCommand Output".

Also take a look at https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_s3_code_examples.html#basics.

As part of #6691, we will be creating additional documentation for streaming operations.

@kuhe kuhe closed this as completed Dec 2, 2024
@kuhe kuhe added duplicate This issue is a duplicate. and removed needs-triage This issue or PR still needs to be triaged. labels Dec 2, 2024
@kuhe kuhe changed the title (short issue description) S3 GetObject returns a stream body Dec 2, 2024
@GeorgeBrn
Copy link
Author

There is nothing readable in there, I am not a beginner programmer and the usage example isn't a real code example... Just a bunch of convoluted text with no meaning. Every other Nodejs SDK out there shows you how to implement, but Amazon is way above that.

---------------------------------ACTUAL CODE EXAMPLE---------------------------------------------
async function getObjectFromS3() {
try {
const { Body } = await s3Client.send(
new GetObjectCommand({
Bucket: "YOUR BUCKET NAME",
Key: "YOUR KEY",
})
);

const chunks = [];
Body.on("data", (chunk) => {
  chunks.push(chunk);
});

return new Promise((resolve, reject) => {
  Body.on("end", () => {
    const buffer = Buffer.concat(chunks);
    resolve(buffer); // Return the image buffer
  });

  Body.on("error", (err) => {
    reject(err); // Reject the promise on error
  });
});

} catch (error) {
console.error("Error getting file", error); // Handle and log errors
}
}

Is it really that hard to paste this in there?

@kuhe
Copy link
Contributor

kuhe commented Dec 3, 2024

Using the example from https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_s3_code_examples.html#basics, you do not need to concatenate into a buffer yourself.

import {
  GetObjectCommand,
  S3Client,
} from "@aws-sdk/client-s3";

const response = await client.send(
  new GetObjectCommand({
    Bucket: bucketName,
    Key: key,
  }),
);

const str = await response.Body.transformToString();
console.log(str); // a string.

Alternatively, a byte array

// Uint8Array, which can be converted to Buffer.
const bytes = await response.Body.transformToByteArray();

@kuhe
Copy link
Contributor

kuhe commented Dec 3, 2024

Currently, the developer guide linked above contains more practical examples than the auto-generated API shape documentation you have seen.

We plan to integrate this example into the API docs in a future update.

@GeorgeBrn
Copy link
Author

GeorgeBrn commented Dec 4, 2024 via email

@GeorgeBrn
Copy link
Author

GeorgeBrn commented Dec 4, 2024 via email

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation This is a problem with documentation. duplicate This issue is a duplicate.
Projects
None yet
Development

No branches or pull requests

2 participants