-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples(storage-control): add folders samples (#26021)
- Loading branch information
1 parent
bfed1dd
commit 2a71a7e
Showing
7 changed files
with
274 additions
and
2 deletions.
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__ |