I would like to do something like (e.g. get both Metadata and a read stream for the body):
const data = await client
.getObject(buildParams(key))
.promise()
console.log(data.Metadata)
data.Body.pipe(process.stdout)
Is something like that possible? I know I could do client.getObject(buildParams(key)).createReadStream() but then I need another request to get the Metadata field.
@olalonde
You would need to attach an event listener onto the request that gets returned when calling a service operation by the SDK. The SDK handles converting the headers received into fields on the data object, but when using streams that step is skipped. In your scenario, you would either need to make a separate 'headObject' call, or you can parse the headers manually using the below as a starting point:
var request = client.getObject(buildParams(key));
request.on('httpHeaders', function(statusCode, headers, response) {
// metadata gets returned as headers that start with 'x-amz-meta-'
// write code to grab the metadata from those headers and do something with them.
});
});
// create the read stream for the body
request.createReadStream().pipe(process.stdout);
Thanks. I'd still like to request a feature that could make it possible for the Body returned by getObject be a readable stream. That would be more similar to how whatwg-fetch and most node.js http client libraries handle this common situation.
@olalonde and @chrisradek Thanks for asking about this as I have a use case exactly like this. Do you know if there has been any feature development like you suggested? It would by nice to simply get the metadata in JSON rather than having to scrape it out from the http headers as explained above.
@adueck
The next major version of the AWS SDK will set the Body field to a stream if the operation supports a streaming response, as is the case with s3.getObject. The next major version of the SDK is currently in developer preview and the S3 package needs to be generated, but feel free to try it out and leave feedback!
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.
Most helpful comment
@adueck
The next major version of the AWS SDK will set the
Bodyfield to a stream if the operation supports a streaming response, as is the case with s3.getObject. The next major version of the SDK is currently in developer preview and the S3 package needs to be generated, but feel free to try it out and leave feedback!