samples: add rename_file.go sample#5451
Conversation
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
Summary of ChangesHello @nidhiii-27, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Go sample for Google Cloud Storage, focusing on the atomic renaming of objects. The primary goal is to provide a clear example of how to leverage the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new Go sample, rename_file.go, demonstrating how to atomically rename an object in a Google Cloud Storage bucket using the MoverFrom feature. The code is clear and follows the existing patterns in the repository.
My review includes two main points. First, I've noted that the new sample is missing corresponding tests. Adding a test case to objects_test.go is crucial for ensuring the sample's correctness and maintainability over time. Second, I suggest adding a precondition to the rename operation to prevent accidentally overwriting existing files, which makes the sample more robust and educational.
| // renameFile renames a file in a Google Cloud Storage bucket. | ||
| // This operation is only available for buckets with the Hierarchical Namespace | ||
| // feature enabled. | ||
| func renameFile(w io.Writer, bucket, srcObject, destObject string) error { |
There was a problem hiding this comment.
This new sample function renameFile is not covered by any tests in objects_test.go. To ensure the sample works as expected and to prevent future regressions, please add a system test for it. This test should verify that the source object is removed and the destination object is created after the rename operation. Given that this feature requires Hierarchical Namespace to be enabled, the test setup might need to create a bucket with this feature specifically.
| src := client.Bucket(bucket).Object(srcObject) | ||
| dst := client.Bucket(bucket).Object(destObject) |
There was a problem hiding this comment.
For a more robust and idiomatic sample, it's good practice to demonstrate the use of preconditions to prevent race conditions or accidental data loss. In this case, you could add a DoesNotExist precondition on the destination object to prevent overwriting an existing file. This is a common and important pattern when renaming or moving files.
src := client.Bucket(bucket).Object(srcObject)
dst := client.Bucket(bucket).Object(destObject)
// Optional: set a precondition to avoid potential race conditions and data
// corruptions. The request to rename the file is aborted if the
// destination object already exists.
dst = dst.If(storage.Conditions{DoesNotExist: true})
NimJay
left a comment
There was a problem hiding this comment.
Hi @nidhiii-27, thanks for this sample.
Question: Did you mean for this pull-request to still be in draft mode? Or is it ready for review?
Nonetheless, I've provided some initial feedback.
| @@ -0,0 +1,59 @@ | |||
| // Copyright 2024 Google LLC | |||
There was a problem hiding this comment.
| // Copyright 2024 Google LLC | |
| // Copyright 2026 Google LLC |
| "io" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/storage" |
There was a problem hiding this comment.
The error in CI:
Error: vet: objects/rename_file.go:51:15: dst.MoverFrom undefined (type *"cloud.google.com/go/storage".ObjectHandle has no field or method MoverFrom)
Do we need to bump the version in the go.mod file?
| return nil | ||
| } | ||
|
|
||
| // [END storage_rename_file] |
There was a problem hiding this comment.
The CONTRIBUTING.md suggests that all samples need to be tested.
See /storage/objects/objects_test.go for inspiration.
Should we add a test for this sample?
This pull request adds a new sample,
rename_file.go, to thestorage/objectsdirectory.This sample demonstrates how to use the new
Movefeature in the Go Google Cloud Storage client library to atomically rename an object within a bucket. The sample includes comments highlighting that this feature requires the Hierarchical Namespace feature to be enabled on the bucket.