Google Cloud Storage C++ Client  1.24.0
A C++ Client Library for Google Cloud Storage
object_stream.cc
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 
16 #include "google/cloud/storage/internal/object_requests.h"
17 #include "google/cloud/log.h"
18 #include <sstream>
19 #include <thread>
20 
21 namespace google {
22 namespace cloud {
23 namespace storage {
24 inline namespace STORAGE_CLIENT_NS {
25 static_assert(std::is_move_assignable<ObjectReadStream>::value,
26  "storage::ObjectReadStream must be move assignable.");
27 static_assert(std::is_move_constructible<ObjectReadStream>::value,
28  "storage::ObjectReadStream must be move constructible.");
29 
30 ObjectReadStream::~ObjectReadStream() {
31  if (!IsOpen()) {
32  return;
33  }
34 #if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
35  try {
36  Close();
37  } catch (std::exception const& ex) {
38  GCP_LOG(INFO) << "Ignored exception while trying to close stream: "
39  << ex.what();
40  } catch (...) {
41  GCP_LOG(INFO) << "Ignored unknown exception while trying to close stream";
42  }
43 #else
44  Close();
45 #endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
46 }
47 
48 void ObjectReadStream::Close() {
49  if (!IsOpen()) {
50  return;
51  }
52  buf_->Close();
53  if (!status().ok()) {
54  setstate(std::ios_base::badbit);
55  }
56 }
57 
58 ObjectWriteStream::ObjectWriteStream(
59  std::unique_ptr<internal::ObjectWriteStreambuf> buf)
60  : std::basic_ostream<char>(nullptr), buf_(std::move(buf)) {
61  init(buf_.get());
62  // If buf_ is already closed, update internal state to represent
63  // the fact that no more bytes can be uploaded to this object.
64  if (!buf_->IsOpen()) {
65  CloseBuf();
66  }
67 }
68 
70  if (!IsOpen()) {
71  return;
72  }
73  // Disable exceptions, even if the application had enabled exceptions the
74  // destructor is supposed to mask them.
75  exceptions(std::ios_base::goodbit);
76  Close();
77 }
78 
80  if (!buf_) {
81  return;
82  }
83  CloseBuf();
84 }
85 
86 void ObjectWriteStream::CloseBuf() {
87  auto response = buf_->Close();
88  if (!response.ok()) {
89  metadata_ = std::move(response).status();
90  setstate(std::ios_base::badbit);
91  return;
92  }
93  headers_ = {};
94  if (response->payload.has_value()) {
95  metadata_ = *std::move(response->payload);
96  }
97  if (metadata_ && !buf_->ValidateHash(*metadata_)) {
98  setstate(std::ios_base::badbit);
99  }
100 }
101 
102 void ObjectWriteStream::Suspend() && { buf_.reset(); }
103 
104 } // namespace STORAGE_CLIENT_NS
105 } // namespace storage
106 } // namespace cloud
107 } // namespace google
google::cloud::storage::v1::ObjectWriteStream::Suspend
void Suspend() &&
Suspends an upload.
Definition: object_stream.cc:102
google::cloud::storage::v1::ObjectWriteStream::~ObjectWriteStream
~ObjectWriteStream() override
Closes the stream (if necessary).
Definition: object_stream.cc:69
google::cloud::storage::v1::ObjectWriteStream::Close
void Close()
Close the stream, finalizing the upload.
Definition: object_stream.cc:79
log.h
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
STORAGE_CLIENT_NS
#define STORAGE_CLIENT_NS
Definition: version.h:22
GCP_LOG
#define GCP_LOG(level)
object_stream.h
google