Skip to content

Instantly share code, notes, and snippets.

@connormanning
Last active April 27, 2016 14:40
Show Gist options
  • Save connormanning/c1b35627e4263efdb7280c625abfe3ea to your computer and use it in GitHub Desktop.
Save connormanning/c1b35627e4263efdb7280c625abfe3ea to your computer and use it in GitHub Desktop.
ArbiterStream
#include <fstream>
#include <memory>
#include <string>
#include <arbiter/arbiter.hpp>
class ArbiterStream : public std::ofstream
{
public:
ArbiterStream(std::string remotePath, std::ios::openmode mode)
: std::ofstream(getLocalFilename(remotePath).c_str(), mode)
, m_remotePath(remotePath)
{ }
std::string getLocalFilename(std::string remotePath)
{
const std::string basename(arbiter::Arbiter::getBasename(remotePath));
const std::string tempdir(arbiter::fs::getTempPath());
const std::string localpath(tempdir + "/" + basename);
std::cout << "creating localpath: " << localpath << std::endl;
return localpath;
}
virtual ~ArbiterStream()
{
close();
arbiter::Arbiter a;
std::cout << "~pushing to " << m_remotePath << std::endl;
a.put(m_remotePath, a.getBinary(getLocalFilename(m_remotePath)));
}
private:
std::string m_remotePath;
};
int main()
{
const std::string remotePath("s3://entwine/abc.txt");
const std::ios_base::openmode binaryTruncMode(
std::ofstream::binary |
std::ofstream::out |
std::ofstream::trunc);
std::unique_ptr<ArbiterStream> s(
new ArbiterStream(remotePath, binaryTruncMode));
*s << "Here's some text..." << std::endl;
std::cout << "DTORing" << std::endl;
s.reset();
std::cout <<
"Done - checking contents from remote:\n\t" <<
arbiter::Arbiter().get(remotePath) << std::endl;
}
$ g++ -std=c++11 stream.cpp -larbiter && ./a.out
creating localpath: /var/folders/_j/dwslpxgs23794lm9hzscrk280000gn/T///abc.txt
DTORing
~pushing to s3://entwine/abc.txt
creating localpath: /var/folders/_j/dwslpxgs23794lm9hzscrk280000gn/T///abc.txt
Done - checking contents from remote:
Here's some text...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment