Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use AWS S3 C++ SDK TransferManager DownloadFile Callback

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?

like image 791
Alex Rablau Avatar asked Dec 13 '16 15:12

Alex Rablau


1 Answers

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.

like image 104
Bret Ambrose Avatar answered Sep 19 '22 12:09

Bret Ambrose