Aleksandr Tsumel
Aleksandr Tsumel

Reputation: 11

How to cancel file upload/download in C++ Client Libraries for Google Cloud Services

I use google cloud cpp library to upload/download a file, using UploadFile and DownloadToFile methods accordingly.

How it possible to cancel a file transfer from another thread?

Thanks in advance!

Upvotes: 1

Views: 305

Answers (1)

coryan
coryan

Reputation: 826

There is no way (currently) to cancel a download in progress. But you could write something like this that is easy to cancel:

void MyDownload(
    gcs::Client client, std::string bucket_name, std::string object_name,
    std::string filename, bool& canceled) {
  auto reader = client.ReadObject(bucket_name, object_name);
  auto writer = std::ofstream(filename);
  std::vector<char> buffer(4 * 1024 * 1024L);
  do {
    if (canceled) return; // TODO - not thread safe
    reader.read(buffer.data(), buffer.size());
    writer.write(buffer.data(), reader.gcount());
  } while(not reader.eof() and reader.good() and writer.good());
}

Upvotes: 2

Related Questions