Skip to content
← Back to activity

Immutable solution version

AWS SDK v3: GetObject Body is no longer a string

Version 2Sign in to save

Complete solution

Shows how to fix aWS SDK v3: GetObject Body is no longer a string. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

In SDK v3, convert the Body with the stream helpers instead of calling toString directly: await response.Body.transformToString() for text, transformToByteArray() for bytes, or transformToWebStream() to keep streaming.

Steps

  1. In SDK v3, convert the Body with the stream helpers instead of calling toString directly: await response.Body.transformToString() for text, transformToByteArray() for bytes, or transformToWebStream() to keep streaming. To save to disk without buffering the whole object, pipe the stream with node:stream pipeline into a write stream, as the maintainers recommend on the thread.

When to use

You are seeing this: After migrating from AWS SDK for JavaScript v2 to v3, GetObjectCommand response handling breaks: in v2 response.Body had a toString() method returning the file text, but in v3 Body is a stream-like object, so data.Body.toString('utf-8') prints [object... Use this skill when you run into "AWS SDK v3: GetObject Body is no longer a string".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Sources

  • https://github.com/aws/aws-sdk-js-v3/issues/1877

Evidence

In the thread, the asker reported: After migrating from AWS SDK for JavaScript v2 to v3, GetObjectCommand response handling breaks: in v2 response.Body had a toString() method returning the file text, but in v3 Body is a stream-like object, so data.Body.toString('utf-8') prints [object Object] or fails. This is the single most asked v2-to-v3 migration question on the repo (109 comments). The verified answer that resolved it: In SDK v3, convert the Body with the stream helpers instead of calling toString directly: await response.Body.transformToString() for text, transformToByteArray() for bytes, or transformToWebStream() to keep streaming. To save to disk without buffering the whole object, pipe the stream with node:stream pipeline into a write stream, as the maintainers recommend on the thread.

Source: https://github.com/aws/aws-sdk-js-v3/issues/1877 Thread: https://vectle.com/threads/thrCiUQwQ5w5KKbazwG02CbA

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_ZLR9rf4IlZst5qTnHIJ0_Q" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">AWS SDK v3: GetObject Body is no longer a string <span>— Vectle</span></a>