-
Notifications
You must be signed in to change notification settings - Fork 589
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-client] PutObjectCommandInput.Body accepts Blob but not ArrayBuffer #5361
Comments
Hi @ImRodry, if you are working on NodeJS runtime then, the Body needs to be provided as a Readable type, because is the type set for this parameter which you can confirm here and here. If you are working on browser runtime then, the Body can be provided as Blob, however, please be aware that Blob type from browser is different than the Blob type in NodeJs. So please, be sure that the value passed as the Body is a valid readable type and you should not have issues. Please let me know if that helps! Thanks! |
Hey @yenfryherrerafeliz! That is actually not true, you're looking at the wrong file. Those types come from here and are defined as |
@ImRodry, you are right, I was wrong about the typing. However, I still do not understand why this should be an issue?. The types we accept in NodeJS runtime are "string | Uint8Array | Buffer | Readable", which does not include ArrayBuffer. ArrayBuffer is a standard JavaScript object for representing binary data regardless environment, but we do not consider it as a type to be used here. Of course, in nodejs you can do the following: Buffer.from(arrayBuffer) and then you should not have any issues. Regarding not accepting Blob type, we actually do accept Blob type but just in browser runtime, and as I mentioned before Blob type in browser is not the same as in NodeJS. I look forward to your response. Thanks! |
The reason why I'm saying it should be accepted is that I am passing an ArrayBuffer obtained directly from Node's fetch library and it's working perfectly, but the types say it shouldn't, however, if I instead use res.blob() I get a runtime error even though the types pass without an issue. |
Any SDK client has a config field called The request and response payloads' acceptable range of types depends on the implementation. We use the superset union because we do not know the types ahead of time. Blob exists in later Node.js versions, but it is not an input type for the default Node.js http handler based on The lack of ArrayBuffer in the declared list means that we don't support it, not that it must not work. You can use a type override to pass it in if you want to use that type. With this change: smithy-lang/smithy-typescript#1046 You can use a type helper from the SDK to narrow the input and output payload types. import { S3 } from "@aws-sdk/client-s3";
import { NodeJsClient } from "@smithy/types";
const s3 = new S3({}) as NodeJsClient<S3>;
await s3.putObject({
Bucket: '',
Key: '',
Body: new Blob() // helper makes this a type error, must use Node.js http(s) payload types like Buffer/Uint8Array/string
})
const get = await s3.getObject({
Bucket: '',
Key: ''
});
const body = get.Body; // helper narrows this to SdkStream<IncomingMessage> |
I'm sorry but that doesn't make any sense. How are we supposed to upload fetched files otherwise? I appreciate the PR though I'm not sure if it will work, but that is not the main goal of this issue at all, the goal is for the types to accept ArrayBuffer |
We may support ArrayBuffer as an input type in the future, but it is not planned. |
What’s stopping you from supporting it though? It already works |
Hi @ImRodry, I am going to mark this issue as a feature request so we can evaluate this further in the future. I appreciate the time you took reporting this and also pitching for why this should be a good type to accept. As workaround for now please use Buffer.from(arrayBuffer). Please let me know if you have any questions. Thanks! |
sure then, but I looked briefly at the Node.js code and it seems like doing Buffer.from() with an ArrayBuffer just creates a Uint8Array with the same data, thus why it's a constant time operation. There would be nothing else needed in order to support ArrayBuffers when you already support Buffers and Uint8Arrays |
Update: sending a ReadableStream (obtained through Response.body) also works though the types say it doesn't. |
Because of our Node.js supported version range, we do not support |
and why would you close this? There is still the initial issue I mentioned, the types are all wrong. It's also not a matter of supporting, it's already supported you just have to update the types |
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. |
Checkboxes for prior research
Describe the bug
The types for PutObjectCommandInput.Body accept Blob as a valid type (when it isn't) and don't accept ArrayBuffer (and they should)
SDK version number
@aws-sdk/[email protected]
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
Node.js v20.8.0
Reproduction Steps
Observed Behavior
There was a TS error when using ArrayBuffer and not when using Blob
Expected Behavior
ArrayBuffer should pass type checks and Blob shouldn't
Possible Solution
Just simply fix the types, I'm just not sure where they are
Additional Information/Context
No response
The text was updated successfully, but these errors were encountered: