Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
|
Defines a std::basic_ostream<char>
to write to a GCS object.
More...
#include <google/cloud/storage/object_write_stream.h>
Public Member Functions | |
ObjectWriteStream () | |
Creates a stream not associated with any buffer. More... | |
ObjectWriteStream (std::unique_ptr< internal::ObjectWriteStreambuf > buf) | |
Creates a stream associated with the give request. More... | |
ObjectWriteStream (ObjectWriteStream &&rhs) noexcept | |
ObjectWriteStream & | operator= (ObjectWriteStream &&rhs) noexcept |
void | swap (ObjectWriteStream &rhs) |
ObjectWriteStream (ObjectWriteStream const &)=delete | |
ObjectWriteStream & | operator= (ObjectWriteStream const &)=delete |
~ObjectWriteStream () override | |
Closes the stream (if necessary). More... | |
bool | IsOpen () const |
Return true if the stream is open to write more data. More... | |
void | Close () |
Close the stream, finalizing the upload. More... | |
std::string const & | resumable_session_id () const |
Returns the resumable upload session id for this upload. More... | |
std::uint64_t | next_expected_byte () const |
Returns the next expected byte. More... | |
void | Suspend () && |
Suspends an upload. More... | |
Status | last_status () const |
Returns the status of partial errors. More... | |
StatusOr< ObjectMetadata > const & | metadata () const & |
Access the upload results. More... | |
StatusOr< ObjectMetadata > && | metadata () && |
Access the upload results. More... | |
std::string const & | received_hash () const |
The received CRC32C checksum and the MD5 hash values as reported by GCS. More... | |
std::string const & | computed_hash () const |
The locally computed checksum and hashes, as a string. More... | |
HeadersMap const & | headers () const |
The headers (if any) returned by the service. More... | |
std::string const & | payload () const |
The returned payload as a raw string, for debugging only. More... | |
Defines a std::basic_ostream<char>
to write to a GCS object.
This class is used to upload objects to GCS. It can handle objects of any size, but keep the following considerations in mind:
Client::InsertObject()
; it is simpler and faster in most cases.operator<<
APIs.The maximum size of this internal buffer is configured using UploadBufferSizeOption
. As with all options, this can be set when the Client object is created. The current default value is 8 MiB, but this default value can change. If the size of this buffer is important for your application please set the value explicitly. You can also provide an override when calling Client::WriteObject()
. Note that this setting is expressed in bytes, but it is always rounded (up) to a multiple of the upload quantum.
On a .write()
call this class attempts to send the data immediately. That is, without copying it to the internal buffer. If any previously buffered data and the data provided in the .write()
call are larger than the maximum size of the internal buffer then the largest amount of data that is a multiple of the upload quantum is flushed. Any data in excess of a multiple of the upload quantum are buffered for the next upload.
These examples may clarify how this works:
ObjectWriteStream
, configured to buffer at most 256KiB. Assume this stream receives a .write()
call with 257 KiB of data. The first 256 KiB are immediately sent and the remaining 1 KiB is buffered for a future upload..write()
call with 256 KiB then it will send the buffered 1 KiB of data and the first 255 KiB from the new buffer. The last 1 KiB is buffered for a future upload.ObjectWriteStream
, configured to buffer at most 256KiB. If this stream receives a .write()
call with 4 MiB of data the data is sent immediately. No data is buffered, as the data size is a multiple of the upload quantum..write()
call with 1024 KiB then both the 256 KiB and the 1024 KiB of data are flushed.When performing formatted I/O, typically used via operator<<
, this class will buffer data based on the UploadBufferSizeOption setting.
For best performance uploading data we recommend using exclusively the unbuffered I/O API. Furthermore, we recommend that applications use data in multiples of the upload quantum in all calls to .write()
. Larger buffers result in better performance. Our empirical results show that these improvements tapper off around 32MiB or so.
If you are planning to use unbuffered I/O, and you are already planning to provide large buffers in the .write()
calls, then there is no need to configure a large value for UploadBufferSizeOption
. As described above, calling .write()
with more data than the UploadBufferSizeOption
immediately flushes the data and only leaves any non-multiple of 256 KiB in the internal buffer.
As it is customary in C++, the destructor of this class finalizes the upload. If you want to prevent the class from finalizing an upload, use the Suspend()
function.
google::cloud::storage::ObjectWriteStream::ObjectWriteStream | ( | ) |
Creates a stream not associated with any buffer.
Attempts to use this stream will result in failures.
|
explicit |
Creates a stream associated with the give request.
Reading from the stream will result in http requests to get more data from the GCS object.
buf | an initialized ObjectWriteStreambuf to upload the data. |
|
noexcept |
|
delete |
|
override |
Closes the stream (if necessary).
void google::cloud::storage::ObjectWriteStream::Close | ( | ) |
Close the stream, finalizing the upload.
Closing a stream completes an upload and creates the uploaded object. On failure it sets the badbit
of the stream.
The metadata of the uploaded object, or a detailed error status, is accessible via the metadata()
member function. Note that the metadata may be empty if the application creates a stream with the Fields("")
parameter, applications cannot assume that all fields in the metadata are filled on success.
std::ios_base::failure | if the application has enabled the exception mask. |
|
inline |
The locally computed checksum and hashes, as a string.
This object computes the CRC32C checksum and MD5 hash of the uploaded data. There are several cases where these values may be empty or irrelevant, for example:
The string has the same format as the value returned by received_hash()
. Note that the format of this string is also subject to change without notice.
|
inline |
The headers (if any) returned by the service.
For debugging only.
|
inline |
Return true if the stream is open to write more data.
|
inline |
Returns the status of partial errors.
Application may write multiple times before closing the stream, this function gives the capability to find out status even before stream closure.
This function is different than metadata()
as calling metadata()
before Close() is undefined.
|
inline |
Access the upload results.
Note that calling these member functions before Close()
is undefined behavior.
|
inline |
Access the upload results.
Note that calling these member functions before Close()
is undefined behavior.
|
inline |
Returns the next expected byte.
For non-resumable uploads this is always zero. Applications that use resumable uploads can use this value to resend any data not committed in the GCS.
|
inlinenoexcept |
|
delete |
|
inline |
The returned payload as a raw string, for debugging only.
|
inline |
The received CRC32C checksum and the MD5 hash values as reported by GCS.
When the upload is finalized (via Close()
) the GCS server reports the CRC32C checksum and, if the object is not a composite object, the MDF hash of the uploaded data. This class compares the reported hashes against locally computed hash values, and reports an error if they do not match.
The values are reported as comma separated tag=value
pairs, e.g. crc32c=AAAAAA==,md5=1B2M2Y8AsgTpgAmY7PhCfg==
. The format of this string is subject to change without notice, they are provided for informational purposes only.
|
inline |
Returns the resumable upload session id for this upload.
Note that this is an empty string for uploads that do not use resumable upload session ids. Client::WriteObject()
enables resumable uploads based on the options set by the application.
void google::cloud::storage::ObjectWriteStream::Suspend | ( | ) | && |
Suspends an upload.
This is a destructive operation. Using this object after calling this function results in undefined behavior. Applications should copy any necessary state (such as the value resumable_session_id()
) before calling this function.
|
inline |