I'm trying to use the AWS C++ SDK and Aws::Transfer::TransferManager to download a file from S3 into memory.
The function I want to use is:
std::shared_ptr< TransferHandle > DownloadFile (const Aws::String &bucketName, const Aws::String &keyName, CreateDownloadStreamCallback writeToStreamfn)
However I'm not sure how the CreateDownloadStreamCallback argument is supposed to work.
CreateDownloadStreamCallback is a typedef of
std::function<Aws::IOStream*(void)>
I'm not sure what should go into this callback function to create and return an Aws::IOStream.
How is this callback function supposed to work?
The intent of the callback function is to delay stream creation until after the request has succeeded. If the request fails then the function never gets called.
It's straightforward to do this via a lambda, so for your case you might do something like:
auto creationFunction = [](){ return Aws::New< Aws::StringStream >( "DownloadTag" ); };
auto transferHandle = transferClient.DownloadFile("MyBucket", "MyKey", creationFunction);
If you wanted to download to a file, you'd switch the creation function to something like:
auto creationFunction = [](){ return Aws::New< Aws::OFStream >( "DownloadTag", "MyFile.txt", std::ofstream::out ); };
On a successful request, the creation function will be invoked and the request body will be streamed into what was created. You will need to be careful with ios flags on the stream. A common error is putting text into a binary stream or vice versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With