-
Notifications
You must be signed in to change notification settings - Fork 556
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
samples(storage-control): add folders samples #26021
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
68 changes: 68 additions & 0 deletions
68
google-cloud-storage-control/samples/acceptance/storage_control_folders_test.rb
This file contains 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,68 @@ | ||
# Copyright 2024 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_control_create_folder" | ||
require_relative "../storage_control_get_folder" | ||
require_relative "../storage_control_list_folders" | ||
require_relative "../storage_control_rename_folder" | ||
require_relative "../storage_control_delete_folder" | ||
|
||
describe "Storage Control Folders" do | ||
let(:bucket_name) { random_bucket_name } | ||
let(:folder_name) { random_folder_name } | ||
|
||
before :all do | ||
create_bucket_helper bucket_name, uniform_bucket_level_access: true, | ||
hierarchical_namespace: { enabled: true } | ||
end | ||
|
||
after do | ||
delete_bucket_helper bucket_name | ||
end | ||
|
||
it "create_folder, get_folder, list_folders, rename_folder, delete_folder" do | ||
# create_folder | ||
out, _err = capture_io do | ||
create_folder bucket_name: bucket_name, folder_name: folder_name | ||
end | ||
|
||
assert_includes out, folder_name | ||
|
||
# list_folders | ||
out, _err = capture_io do | ||
list_folders bucket_name: bucket_name | ||
end | ||
|
||
assert_includes out, "ruby-storage-control-folder-samples-test" | ||
|
||
# get_folder | ||
out, _err = capture_io do | ||
get_folder bucket_name: bucket_name, folder_name: folder_name | ||
end | ||
|
||
assert_includes out, folder_name | ||
|
||
# rename_folder | ||
new_folder_name = "#{folder_name}_new" | ||
assert_output "Renamed folder #{folder_name} to #{new_folder_name}\n" do | ||
rename_folder bucket_name: bucket_name, source_folder_id: folder_name, destination_folder_id: new_folder_name | ||
end | ||
|
||
# delete_folder | ||
assert_output "Deleted folder: #{new_folder_name}\n" do | ||
delete_folder bucket_name: bucket_name, folder_name: new_folder_name | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
google-cloud-storage-control/samples/storage_control_create_folder.rb
This file contains 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,39 @@ | ||
# Copyright 2024 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. | ||
|
||
# [START storage_control_create_folder] | ||
def create_folder bucket_name:, folder_name: | ||
# The ID of your GCS bucket | ||
# bucket_name = "your-unique-bucket-name" | ||
|
||
# The name of the folder to be created | ||
# folder_name = "folder-name" | ||
|
||
require "google/cloud/storage/control" | ||
|
||
storage_control = Google::Cloud::Storage::Control.storage_control | ||
|
||
# The storage bucket path uses the global access pattern, in which the "_" | ||
# denotes this bucket exists in the global namespace. | ||
bucket_path = storage_control.bucket_path project: "_", bucket: bucket_name | ||
|
||
request = Google::Cloud::Storage::Control::V2::CreateFolderRequest.new parent: bucket_path, folder_id: folder_name | ||
|
||
response = storage_control.create_folder request | ||
|
||
puts "Created folder: #{response.name}" | ||
end | ||
# [END storage_control_create_folder] | ||
|
||
create_folder bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
39 changes: 39 additions & 0 deletions
39
google-cloud-storage-control/samples/storage_control_delete_folder.rb
This file contains 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,39 @@ | ||
# Copyright 2024 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. | ||
|
||
# [START storage_control_delete_folder] | ||
def delete_folder bucket_name:, folder_name: | ||
# The ID of your GCS bucket | ||
# bucket_name = "your-unique-bucket-name" | ||
# | ||
# Name of the folder you want to delete | ||
# folder_name = "name-of-the-folder" | ||
|
||
require "google/cloud/storage/control" | ||
|
||
storage_control = Google::Cloud::Storage::Control.storage_control | ||
|
||
# The storage folder path uses the global access pattern, in which the "_" | ||
# denotes this bucket exists in the global namespace. | ||
folder_path = storage_control.folder_path project: "_", bucket: bucket_name, folder: folder_name | ||
|
||
request = Google::Cloud::Storage::Control::V2::DeleteFolderRequest.new name: folder_path | ||
|
||
storage_control.delete_folder request | ||
|
||
puts "Deleted folder: #{folder_name}" | ||
end | ||
# [END storage_control_delete_folder] | ||
|
||
delete_folder bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
39 changes: 39 additions & 0 deletions
39
google-cloud-storage-control/samples/storage_control_get_folder.rb
This file contains 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,39 @@ | ||
# Copyright 2024 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. | ||
|
||
# [START storage_control_get_folder] | ||
def get_folder bucket_name:, folder_name: | ||
# The ID of your GCS bucket | ||
# bucket_name = "your-unique-bucket-name" | ||
|
||
# The name of the folder to be created | ||
# folder_name = "folder-name" | ||
|
||
require "google/cloud/storage/control" | ||
|
||
storage_control = Google::Cloud::Storage::Control.storage_control | ||
|
||
# The storage folder path uses the global access pattern, in which the "_" | ||
# denotes this bucket exists in the global namespace. | ||
folder_path = storage_control.folder_path project: "_", bucket: bucket_name, folder: folder_name | ||
|
||
request = Google::Cloud::Storage::Control::V2::GetFolderRequest.new name: folder_path | ||
|
||
response = storage_control.get_folder request | ||
|
||
puts "Got folder #{response.name}" | ||
end | ||
# [END storage_control_get_folder] | ||
|
||
get_folder bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
36 changes: 36 additions & 0 deletions
36
google-cloud-storage-control/samples/storage_control_list_folders.rb
This file contains 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,36 @@ | ||
# Copyright 2024 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. | ||
|
||
# [START storage_control_list_folders] | ||
def list_folders bucket_name: | ||
# The ID of your GCS bucket | ||
# bucket_name = "your-unique-bucket-name" | ||
|
||
require "google/cloud/storage/control" | ||
|
||
storage_control = Google::Cloud::Storage::Control.storage_control | ||
|
||
# The storage bucket path uses the global access pattern, in which the "_" | ||
# denotes this bucket exists in the global namespace. | ||
bucket_path = storage_control.bucket_path project: "_", bucket: bucket_name | ||
|
||
request = Google::Cloud::Storage::Control::V2::ListFoldersRequest.new parent: bucket_path | ||
|
||
response = storage_control.list_folders request | ||
|
||
puts response.response.folders | ||
end | ||
# [END storage_control_list_folders] | ||
|
||
list_folders bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
43 changes: 43 additions & 0 deletions
43
google-cloud-storage-control/samples/storage_control_rename_folder.rb
This file contains 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,43 @@ | ||
# Copyright 2024 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. | ||
|
||
# [START storage_control_rename_folder] | ||
def rename_folder bucket_name:, source_folder_id:, destination_folder_id: | ||
# The ID of your GCS bucket | ||
# bucket_name = "your-unique-bucket-name" | ||
# | ||
# The source folder ID | ||
# source_folder_id = "current-folder-id" | ||
# | ||
# The destination folder ID, e.g. foo/bar/ | ||
# destination_folder_id = "destination-folder-id" | ||
|
||
require "google/cloud/storage/control" | ||
|
||
storage_control = Google::Cloud::Storage::Control.storage_control | ||
|
||
# The storage folder path uses the global access pattern, in which the "_" | ||
# denotes this bucket exists in the global namespace. | ||
folder_path = storage_control.folder_path project: "_", bucket: bucket_name, folder: source_folder_id | ||
|
||
request = Google::Cloud::Storage::Control::V2::RenameFolderRequest.new name: folder_path, | ||
destination_folder_id: destination_folder_id | ||
|
||
storage_control.rename_folder request | ||
|
||
puts "Renamed folder #{source_folder_id} to #{destination_folder_id}" | ||
end | ||
# [END storage_control_rename_folder] | ||
|
||
rename_folder bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__ |
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.
Thanks for the samples! So this is the specific aspect we want/need to showcase in the handwritten samples, correct?
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.
We usually take advantage of the
paths
helper function to construct the needed path in Ruby. I feel this is helpful but if you have a better way of doing this in your language, that works too.