Google Cloud Storage C++ Client  1.24.0
A C++ Client Library for Google Cloud Storage
object_stream.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_STREAM_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_STREAM_H
17 
18 #include "google/cloud/storage/internal/object_streambuf.h"
21 #include <ios>
22 #include <iostream>
23 #include <string>
24 
25 namespace google {
26 namespace cloud {
27 namespace storage {
28 inline namespace STORAGE_CLIENT_NS {
32 class HashMismatchError : public std::ios_base::failure {
33  public:
34  explicit HashMismatchError(std::string const& msg, std::string received,
35  std::string computed)
36  : std::ios_base::failure(msg),
37  received_hash_(std::move(received)),
38  computed_hash_(std::move(computed)) {}
39 
40  std::string const& received_hash() const { return received_hash_; }
41  std::string const& computed_hash() const { return computed_hash_; }
42 
43  private:
44  std::string received_hash_;
45  std::string computed_hash_;
46 };
47 
51 class ObjectReadStream : public std::basic_istream<char> {
52  public:
58  ObjectReadStream() : std::basic_istream<char>(nullptr), buf_() {}
59 
63  explicit ObjectReadStream(std::unique_ptr<internal::ObjectReadStreambuf> buf)
64  : std::basic_istream<char>(nullptr), buf_(std::move(buf)) {
65  // Initialize the basic_ios<> class
66  init(buf_.get());
67  }
68 
70  : ObjectReadStream(std::move(rhs.buf_)) {
71  // We cannot use set_rdbuf() because older versions of libstdc++ do not
72  // implement this function. Unfortunately `move()` resets `rdbuf()`, and
73  // `rdbuf()` resets the state, so we have to manually copy the rest of
74  // the state.
75  setstate(rhs.rdstate());
76  copyfmt(rhs);
77  rhs.rdbuf(nullptr);
78  }
79 
81  buf_ = std::move(rhs.buf_);
82  // Use rdbuf() (instead of set_rdbuf()) because older versions of libstdc++
83  // do not implement this function. Unfortunately `rdbuf()` resets the state,
84  // and `move()` resets `rdbuf()`, so we have to manually copy the rest of
85  // the state.
86  rdbuf(buf_.get());
87  setstate(rhs.rdstate());
88  copyfmt(rhs);
89  rhs.rdbuf(nullptr);
90  return *this;
91  }
92 
95 
97  ~ObjectReadStream() override;
98 
99  bool IsOpen() const { return static_cast<bool>(buf_) && buf_->IsOpen(); }
100 
104  void Close();
105 
107 
112  Status const& status() const& { return buf_->status(); }
113 
130  std::string const& received_hash() const { return buf_->received_hash(); }
131 
149  std::string const& computed_hash() const { return buf_->computed_hash(); }
150 
152  std::multimap<std::string, std::string> const& headers() const {
153  return buf_->headers();
154  }
156 
157  private:
158  std::unique_ptr<internal::ObjectReadStreambuf> buf_;
159 };
160 
164 class ObjectWriteStream : public std::basic_ostream<char> {
165  public:
171  ObjectWriteStream() : std::basic_ostream<char>(nullptr), buf_() {}
172 
181  explicit ObjectWriteStream(
182  std::unique_ptr<internal::ObjectWriteStreambuf> buf);
183 
185  : ObjectWriteStream(std::move(rhs.buf_)) {
186  metadata_ = std::move(rhs.metadata_);
187  headers_ = std::move(rhs.headers_);
188  payload_ = std::move(rhs.payload_);
189  // We cannot use set_rdbuf() because older versions of libstdc++ do not
190  // implement this function. Unfortunately `move()` resets `rdbuf()`, and
191  // `rdbuf()` resets the state, so we have to manually copy the rest of
192  // the state.
193  setstate(rhs.rdstate());
194  copyfmt(rhs);
195  rhs.rdbuf(nullptr);
196  }
197 
199  buf_ = std::move(rhs.buf_);
200  metadata_ = std::move(rhs.metadata_);
201  headers_ = std::move(rhs.headers_);
202  payload_ = std::move(rhs.payload_);
203  // Use rdbuf() (instead of set_rdbuf()) because older versions of libstdc++
204  // do not implement this function. Unfortunately `rdbuf()` resets the state,
205  // and `move()` resets `rdbuf()`, so we have to manually copy the rest of
206  // the state.
207  rdbuf(buf_.get());
208  setstate(rhs.rdstate());
209  copyfmt(rhs);
210  rhs.rdbuf(nullptr);
211  return *this;
212  }
213 
216 
218  ~ObjectWriteStream() override;
219 
237  bool IsOpen() const { return buf_ != nullptr && buf_->IsOpen(); }
238 
254  void Close();
255 
257 
263  StatusOr<ObjectMetadata> const& metadata() const& { return metadata_; }
264  StatusOr<ObjectMetadata>&& metadata() && { return std::move(metadata_); }
265 
282  std::string const& received_hash() const { return buf_->received_hash(); }
283 
301  std::string const& computed_hash() const { return buf_->computed_hash(); }
302 
304  std::multimap<std::string, std::string> const& headers() const {
305  return headers_;
306  }
307 
309  std::string const& payload() const { return payload_; }
311 
321  std::string const& resumable_session_id() const {
322  return buf_->resumable_session_id();
323  }
324 
332  std::uint64_t next_expected_byte() const {
333  return buf_->next_expected_byte();
334  }
335 
344  void Suspend() &&;
345 
356  Status last_status() const { return buf_->last_status(); }
357 
358  private:
362  void CloseBuf();
363 
364  std::unique_ptr<internal::ObjectWriteStreambuf> buf_;
365  StatusOr<ObjectMetadata> metadata_;
366  std::multimap<std::string, std::string> headers_;
367  std::string payload_;
368 };
369 
370 } // namespace STORAGE_CLIENT_NS
371 } // namespace storage
372 } // namespace cloud
373 } // namespace google
374 
375 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_STREAM_H
google::cloud::storage::v1::ObjectReadStream
Defines a std::basic_istream<char> to read from a GCS Object.
Definition: object_stream.h:51
google::cloud::storage::v1::ObjectWriteStream::computed_hash
std::string const & computed_hash() const
The locally computed checksum and hashes, as a string.
Definition: object_stream.h:301
google::cloud::storage::v1::ObjectWriteStream::resumable_session_id
std::string const & resumable_session_id() const
Returns the resumable upload session id for this upload.
Definition: object_stream.h:321
google::cloud::storage::v1::ObjectWriteStream
Defines a std::basic_ostream<char> to write to a GCS Object.
Definition: object_stream.h:164
google::cloud::storage::v1::ObjectWriteStream::next_expected_byte
std::uint64_t next_expected_byte() const
Returns the next expected byte.
Definition: object_stream.h:332
google::cloud::storage::v1::ObjectReadStream::ObjectReadStream
ObjectReadStream(ObjectReadStream const &)=delete
google::cloud::storage::v1::ObjectWriteStream::ObjectWriteStream
ObjectWriteStream()
Creates a stream not associated with any buffer.
Definition: object_stream.h:171
google::cloud::storage::v1::ObjectReadStream::status
Status const & status() const &
Report any download errors.
Definition: object_stream.h:112
google::cloud::storage::v1::ObjectWriteStream::operator=
ObjectWriteStream & operator=(ObjectWriteStream const &)=delete
google::cloud::storage::v1::ObjectWriteStream::last_status
Status last_status() const
Returns the status of partial errors.
Definition: object_stream.h:356
google::cloud::storage::v1::ObjectReadStream::headers
std::multimap< std::string, std::string > const & headers() const
The headers returned by the service, for debugging only.
Definition: object_stream.h:152
google::cloud::storage::v1::ObjectReadStream::received_hash
std::string const & received_hash() const
The received CRC32C checksum and the MD5 hash values as reported by GCS.
Definition: object_stream.h:130
google::cloud::storage::v1::ObjectWriteStream::received_hash
std::string const & received_hash() const
The received CRC32C checksum and the MD5 hash values as reported by GCS.
Definition: object_stream.h:282
version.h
google::cloud::storage::v1::HashMismatchError
Report checksum mismatches as exceptions.
Definition: object_stream.h:32
google::cloud::storage::v1::ObjectWriteStream::metadata
StatusOr< ObjectMetadata > && metadata() &&
Definition: object_stream.h:264
object_metadata.h
google::cloud::storage::v1::ObjectWriteStream::ObjectWriteStream
ObjectWriteStream(ObjectWriteStream &&rhs) noexcept
Definition: object_stream.h:184
google::cloud::storage::v1::ObjectReadStream::ObjectReadStream
ObjectReadStream()
Creates a stream not associated with any buffer.
Definition: object_stream.h:58
google::cloud::storage::v1::ObjectReadStream::operator=
ObjectReadStream & operator=(ObjectReadStream &&rhs) noexcept
Definition: object_stream.h:80
google::cloud::storage::v1::HashMismatchError::computed_hash
std::string const & computed_hash() const
Definition: object_stream.h:41
google::cloud::storage::v1::ObjectWriteStream::IsOpen
bool IsOpen() const
Return true if the stream is open to write more data.
Definition: object_stream.h:237
google::cloud::storage::v1::ObjectWriteStream::ObjectWriteStream
ObjectWriteStream(ObjectWriteStream const &)=delete
google::cloud::storage::v1::ObjectWriteStream::metadata
StatusOr< ObjectMetadata > const & metadata() const &
Access the upload results.
Definition: object_stream.h:263
STORAGE_CLIENT_NS
#define STORAGE_CLIENT_NS
Definition: version.h:22
google::cloud::storage::v1::ObjectWriteStream::payload
std::string const & payload() const
The returned payload as a raw string, for debugging only.
Definition: object_stream.h:309
google::cloud::storage::v1::ObjectWriteStream::headers
std::multimap< std::string, std::string > const & headers() const
The headers returned by the service, for debugging only.
Definition: object_stream.h:304
google::cloud::storage::v1::ObjectReadStream::ObjectReadStream
ObjectReadStream(std::unique_ptr< internal::ObjectReadStreambuf > buf)
Creates a stream associated with the given streambuf.
Definition: object_stream.h:63
google::cloud::storage::v1::HashMismatchError::received_hash
std::string const & received_hash() const
Definition: object_stream.h:40
google::cloud::storage::v1::ObjectReadStream::IsOpen
bool IsOpen() const
Definition: object_stream.h:99
google::cloud::storage::v1::ObjectWriteStream::operator=
ObjectWriteStream & operator=(ObjectWriteStream &&rhs) noexcept
Definition: object_stream.h:198
google::cloud::storage::v1::ObjectReadStream::ObjectReadStream
ObjectReadStream(ObjectReadStream &&rhs) noexcept
Definition: object_stream.h:69
google::cloud::storage::v1::HashMismatchError::HashMismatchError
HashMismatchError(std::string const &msg, std::string received, std::string computed)
Definition: object_stream.h:34
google
google::cloud::storage::v1::ObjectReadStream::operator=
ObjectReadStream & operator=(ObjectReadStream const &)=delete
google::cloud::storage::v1::ObjectReadStream::computed_hash
std::string const & computed_hash() const
The locally computed checksum and hashes, as a string.
Definition: object_stream.h:149