forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Storage batch operation samples v2 #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubhangi-google
wants to merge
19
commits into
main
Choose a base branch
from
storage_batch_operation_samples_v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e5c98bc
changes working
shubhangi-google c530e28
fixing lint
shubhangi-google 3f17366
updating
shubhangi-google 9851c7e
updating
shubhangi-google b274ae4
fixing test cases
shubhangi-google a38fca4
fix test cases
shubhangi-google 474d7e2
fix comments
shubhangi-google 9821e4f
fix test
shubhangi-google c86088c
Update Gemfile
shubhangi-google e2b297f
Update batch_job_test.rb
shubhangi-google 19009a8
Update batch_job_test.rb
shubhangi-google e626edb
Update helper.rb
shubhangi-google fcb8310
adding full License
shubhangi-google 5eddc25
fixing lint
shubhangi-google 60cb50d
fixing lint
shubhangi-google 3289696
using wrapper class
shubhangi-google 578ba5f
Merge branch 'main' into storage_batch_operation_samples
shubhangi-google ce0f80f
Merge branch 'main' into storage_batch_operation_samples
shubhangi-google 2c2f365
refactoring
shubhangi-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
google-cloud-storage_batch_operations/samples/.rubocop.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| inherit_gem: | ||
| google-style: google-style.yml | ||
|
|
||
| Lint/EmptyBlock: | ||
| Enabled: false | ||
| Lint/ShadowingOuterLocalVariable: | ||
| Enabled: false | ||
| Metrics/AbcSize: | ||
| Enabled: false | ||
| Metrics/BlockLength: | ||
| Enabled: false | ||
| Metrics/CyclomaticComplexity: | ||
| Max: 12 | ||
| Metrics/MethodLength: | ||
| Enabled: false | ||
| Naming/AccessorMethodName: | ||
| Enabled: false | ||
| Style/GlobalVars: | ||
| Exclude: | ||
| - "acceptance/helper.rb" | ||
| Style/Next: | ||
| Enabled: false | ||
| Style/SymbolProc: | ||
| Enabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| source "https://rubygems.org" | ||
|
|
||
| if ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master" | ||
| gem "google-cloud-storage", group: :test, path: "../../google-cloud-storage" | ||
| gem "google-cloud-storage_batch_operations", group: :test, path: "../../google-cloud-storage_batch_operations" | ||
| else | ||
| gem "google-cloud-storage" | ||
| gem "google-cloud-storage_batch_operations" | ||
| end | ||
| group :test do | ||
| gem "google-style", "~> 1.30.0" | ||
| gem "minitest", "~> 5.14" | ||
| gem "minitest-focus", "~> 1.1" | ||
| gem "minitest-hooks", "~> 1.5" | ||
| gem "rake" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require "rake/testtask" | ||
|
|
||
| Rake::TestTask.new "test" do |t| | ||
| t.test_files = FileList["**/*_test.rb"] | ||
| t.warning = false | ||
| end |
87 changes: 87 additions & 0 deletions
87
google-cloud-storage_batch_operations/samples/acceptance/batch_job_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require_relative "helper" | ||
| require_relative "../storage_batch_create_job" | ||
| require_relative "../storage_batch_delete_job" | ||
| require_relative "../storage_batch_cancel_job" | ||
| require_relative "../storage_batch_list_job" | ||
| require_relative "../storage_batch_get_job" | ||
|
|
||
| describe "Batch jobs Snippets" do | ||
| let(:project_name) { ENV["GCLOUD_TEST_PROJECT"] } | ||
| let(:bucket) { @bucket } | ||
| let(:file_content) { "some content" } | ||
| let(:remote_file_name) { "ruby_file_#{SecureRandom.hex}" } | ||
|
|
||
| before :all do | ||
| @bucket = create_bucket_helper random_bucket_name | ||
| end | ||
|
|
||
| after :all do | ||
| delete_bucket_helper @bucket.name | ||
| end | ||
|
|
||
| def create_test_job my_job | ||
| bucket.create_file StringIO.new(file_content), remote_file_name | ||
| create_job bucket_name: bucket.name, prefix: "ruby_file", job_name: my_job, project_name: project_name | ||
| end | ||
|
|
||
| describe "storage batch manage operations" do | ||
| before do | ||
| @job_name = "ruby-sbo-job-#{SecureRandom.hex}" | ||
| create_test_job @job_name | ||
| end | ||
|
|
||
| it "lists jobs and includes the created job" do | ||
| out, _err = capture_io { list_job project_name: project_name } | ||
| assert_includes out, @job_name, "Expected job name not found in the result list" | ||
| end | ||
|
|
||
| it "fetches the details of a job" do | ||
| result = get_job project_name: project_name, job_name: @job_name | ||
| assert_includes result, @job_name, "Expected job name not found in the result" | ||
| end | ||
|
|
||
| it "cancels a job" do | ||
| assert_output "The #{@job_name} is canceled.\n" do | ||
| cancel_job project_name: project_name, job_name: @job_name | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "Delete storage batch operation" do | ||
| before do | ||
| @job_name = "ruby-sbo-job-#{SecureRandom.hex}" | ||
| create_test_job @job_name | ||
| end | ||
| it "deletes a job" do | ||
| retry_job_status do | ||
| get_job project_name: project_name, job_name: @job_name | ||
| end | ||
| assert_output "The #{@job_name} is deleted.\n" do | ||
| delete_job project_name: project_name, job_name: @job_name | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "creates a storage batch operation" do | ||
| it "creates a job" do | ||
| @job_name = "ruby-sbo-job-#{SecureRandom.hex}" | ||
| assert_output "The #{@job_name} is created.\n" do | ||
| create_test_job @job_name | ||
| end | ||
| end | ||
| end | ||
| end |
67 changes: 67 additions & 0 deletions
67
google-cloud-storage_batch_operations/samples/acceptance/helper.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| require "google/cloud/errors" | ||
| require "google/cloud/storage" | ||
| require "minitest/autorun" | ||
| require "minitest/focus" | ||
| require "minitest/hooks/default" | ||
| require "net/http" | ||
| require "time" | ||
| require "securerandom" | ||
|
|
||
| def create_bucket_helper bucket_name | ||
| storage_client = Google::Cloud::Storage.new | ||
| retry_resource_exhaustion do | ||
| storage_client.create_bucket bucket_name | ||
| end | ||
| end | ||
|
|
||
| def delete_bucket_helper bucket_name | ||
| storage_client = Google::Cloud::Storage.new | ||
| retry_resource_exhaustion do | ||
| bucket = storage_client.bucket bucket_name | ||
| return unless bucket | ||
|
|
||
| bucket.files.each(&:delete) | ||
| bucket.delete | ||
| end | ||
| end | ||
|
|
||
| def retry_job_status | ||
| 5.times do | ||
| if yield[/job_status-\s*(\w+)/, 1] == "RUNNING" | ||
| puts "Job in Running status. Gonna try again" | ||
| sleep rand(10..16) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def retry_resource_exhaustion | ||
| 5.times do | ||
| return yield | ||
| rescue Google::Cloud::ResourceExhaustedError => e | ||
| puts "\n#{e} Gonna try again" | ||
| sleep rand(10..16) | ||
| rescue StandardError => e | ||
| puts "\n#{e}" | ||
| raise e | ||
| end | ||
| raise Google::Cloud::ResourceExhaustedError, "Maybe take a break from creating and deleting buckets for a bit" | ||
| end | ||
|
|
||
| def random_bucket_name | ||
| t = Time.now.utc.iso8601.gsub ":", "-" | ||
| "ruby-storage-samples-test-#{t}-#{SecureRandom.hex 4}".downcase | ||
| end | ||
38 changes: 38 additions & 0 deletions
38
google-cloud-storage_batch_operations/samples/storage_batch_cancel_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| require "google/cloud/storage_batch_operations" | ||
|
|
||
| # [START storage_batch_cancel_job] | ||
| def cancel_job project_name:, job_name: | ||
| # The Name/ID of your project | ||
| # project_name = "your-project-id" | ||
|
|
||
| # The name of your Storage batch operation job | ||
| # job_name = "your-job-name" | ||
|
|
||
| client = Google::Cloud::StorageBatchOperations.storage_batch_operations | ||
| parent = "projects/#{project_name}/locations/global" | ||
|
|
||
| request = Google::Cloud::StorageBatchOperations::V1::CancelJobRequest.new name: "#{parent}/jobs/#{job_name}" | ||
| result = client.cancel_job request | ||
| message = if result.is_a? Google::Cloud::StorageBatchOperations::V1::CancelJobResponse | ||
| "The #{job_name} is canceled." | ||
| else | ||
| "The #{job_name} is not canceled." | ||
| end | ||
| puts message | ||
| end | ||
| # [END storage_batch_cancel_job] | ||
|
|
||
| cancel_job project_name: ARGV.shift, job_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
74 changes: 74 additions & 0 deletions
74
google-cloud-storage_batch_operations/samples/storage_batch_create_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| require "google/cloud/storage_batch_operations" | ||
|
|
||
| # [START storage_batch_create_job] | ||
| def create_job bucket_name:, prefix:, job_name:, project_name: | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| # Prefix is the first part of filename on which job has to be executed | ||
| # prefix = 'test' | ||
|
|
||
| # The Name/ID of your project | ||
| # project_name = "your-project-id" | ||
|
|
||
| # The name of your Storage batch operation job | ||
| # job_name = "your-job-name" | ||
|
|
||
| client = Google::Cloud::StorageBatchOperations.storage_batch_operations | ||
|
|
||
| parent = "projects/#{project_name}/locations/global" | ||
|
|
||
| prefix_list = Google::Cloud::StorageBatchOperations::V1::PrefixList.new( | ||
| included_object_prefixes: [prefix] | ||
| ) | ||
|
|
||
| bucket = Google::Cloud::StorageBatchOperations::V1::BucketList::Bucket.new( | ||
| bucket: bucket_name, | ||
| prefix_list: prefix_list | ||
| ) | ||
|
|
||
| bucket_list = Google::Cloud::StorageBatchOperations::V1::BucketList.new( | ||
| buckets: [bucket] | ||
| ) | ||
|
|
||
| # Define the delete operation | ||
| delete_object = Google::Cloud::StorageBatchOperations::V1::DeleteObject.new( | ||
| permanent_object_deletion_enabled: false | ||
| ) | ||
|
|
||
| # Build the job | ||
| job = Google::Cloud::StorageBatchOperations::V1::Job.new( | ||
| name: job_name, | ||
| bucket_list: bucket_list, | ||
| delete_object: delete_object | ||
| ) | ||
|
|
||
| request = Google::Cloud::StorageBatchOperations::V1::CreateJobRequest.new parent: parent, job_id: job_name, job: job | ||
| result = client.create_job request | ||
|
|
||
| puts result.is_a?(Gapic::Operation) ? "The #{job_name} is created." : "The #{job_name} is not created." | ||
| end | ||
| # [END storage_batch_create_job] | ||
|
|
||
| if $PROGRAM_NAME == __FILE__ | ||
| create_job( | ||
| bucket_name: ARGV.shift, | ||
| prefix: ARGV.shift, | ||
| job_name: ARGV.shift, | ||
| project_name: ARGV.shift, | ||
| job_type: ARGV.shift | ||
| ) | ||
|
||
| end | ||
32 changes: 32 additions & 0 deletions
32
google-cloud-storage_batch_operations/samples/storage_batch_delete_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| require "google/cloud/storage_batch_operations" | ||
|
|
||
| # [START storage_batch_delete_job] | ||
| def delete_job project_name:, job_name: | ||
| # The Name/ID of your project | ||
| # project_name = "your-project-id" | ||
|
|
||
| # The name of your Storage batch operation job | ||
| # job_name = "your-job-name" | ||
|
|
||
| client = Google::Cloud::StorageBatchOperations.storage_batch_operations | ||
| parent = "projects/#{project_name}/locations/global" | ||
| request = Google::Cloud::StorageBatchOperations::V1::DeleteJobRequest.new name: "#{parent}/jobs/#{job_name}" | ||
| result = client.delete_job request | ||
| puts result.is_a?(Google::Protobuf::Empty) ? "The #{job_name} is deleted." : "The #{job_name} is not deleted." | ||
| end | ||
| # [END storage_batch_delete_job] | ||
|
|
||
| delete_job project_name: ARGV.shift, job_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
retry_job_statusmethod's loop will always run 5 times, regardless of the job's status. If the job is notRUNNING, the loop continues without breaking, which is inefficient and may not reflect the intended waiting logic. The loop should terminate as soon as the job is no longer in aRUNNINGstate to avoid unnecessary iterations and delays.