Skip to content

docs(storage): add samples for PrecomputedChecksumsOption - #16308

Open
v-pratap wants to merge 3 commits into
googleapis:mainfrom
v-pratap:samples-precomputed-checksums
Open

docs(storage): add samples for PrecomputedChecksumsOption#16308
v-pratap wants to merge 3 commits into
googleapis:mainfrom
v-pratap:samples-precomputed-checksums

Conversation

@v-pratap

@v-pratap v-pratap commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

This PR updates the examples to demonstrate how to use PrecomputedChecksumsOption.

  • Async Sample: Replaced the old UseCrc32cValueOption in storage_async_samples.cc with PrecomputedChecksumsOption.
  • Sync Sample: Added InsertObjectWithChecksum and WriteObjectWithChecksum to storage_object_samples.cc to demonstrate providing precomputed hashes to synchronous client calls.

These changes are a follow-up to the previously merged PR that introduced PrecomputedChecksumsOption.

Use the new PrecomputedChecksumsOption instead of UseCrc32cValueOption in the async upload sample.
Added  and  examples to demonstrate how to provide precomputed hashes to synchronous client calls.
@v-pratap
v-pratap requested review from a team as code owners August 3, 2026 07:50
@product-auto-label product-auto-label Bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels Aug 3, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Google Cloud Storage C++ examples to demonstrate the use of precomputed checksums when inserting and writing objects. It replaces the deprecated UseCrc32cValueOption with PrecomputedChecksumsOption in async samples and introduces new synchronous examples (InsertObjectWithChecksum and WriteObjectWithChecksum). The review feedback highlights a misleading use of std::move on a const reference string parameter in InsertObjectWithChecksum, which could also lead to an order-of-evaluation bug if the parameter is ever refactored to be passed by value.

Comment on lines +155 to +159
StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject(
bucket_name, object_name, std::move(contents),
google::cloud::Options{}.set<gcs::PrecomputedChecksumsOption>(
gcs::PrecomputedChecksums{
gcs::ComputeCrc32cChecksum(contents), ""}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using std::move(contents) on a std::string const& is misleading because it does not actually move the string (it results in a copy). More importantly, if contents is ever refactored to be passed by value (e.g., std::string contents) to allow a true move, this code introduces a severe order-of-evaluation bug. Because the order of evaluation of function arguments is indeterminately sequenced, std::move(contents) could be evaluated before gcs::ComputeCrc32cChecksum(contents), causing the checksum to be computed on an empty, moved-from string.

To prevent this, compute the checksum first and store it in a local variable, then pass contents without the misleading std::move.

    auto checksum = gcs::ComputeCrc32cChecksum(contents);
    StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject(
        bucket_name, object_name, contents,
        google::cloud::Options{}.set<gcs::PrecomputedChecksumsOption>(
            gcs::PrecomputedChecksums{std::move(checksum), ""}));

- Remove empty string explicitly passed for MD5 hash in precomputed checksums.
- Add an example demonstrating .
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.31%. Comparing base (6f13a6b) to head (c447204).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16308      +/-   ##
==========================================
+ Coverage   92.29%   92.31%   +0.01%     
==========================================
  Files        2226     2226              
  Lines      208573   208851     +278     
==========================================
+ Hits       192504   192792     +288     
+ Misses      16069    16059      -10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

[](gcs::Client client, std::string const& bucket_name,
std::string const& object_name, std::string const& contents) {
try {
client.InsertObject(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client.InsertObject(...) returns a StatusOr<ObjectMetadata>, and it does not throw exceptions by default when an API error occurs. Because the returned StatusOr is ignored, the bad checksum error will be silently dropped, and the catch block will never execute.

[](gcs::Client client, std::string const& bucket_name,
std::string const& object_name, std::string const& contents) {
StatusOr<gcs::ObjectMetadata> object_metadata = client.InsertObject(
bucket_name, object_name, std::move(contents),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling std::move on a const reference doesn't move anything; it forces a copy. This appears to be inherited from the original InsertObject snippet, but it is an anti-pattern, so let's avoid it. Here and below.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants