Skip to content

Commit

Permalink
samples(storage-control): add folders samples (#26021)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajajneha27 authored Jun 7, 2024
1 parent bfed1dd commit 2a71a7e
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 2 deletions.
12 changes: 10 additions & 2 deletions google-cloud-storage-control/samples/acceptance/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ def random_bucket_name
"ruby-storage-control-samples-test-#{t}-#{SecureRandom.hex 4}".downcase
end

def create_bucket_helper bucket_name
def random_folder_name
t = Time.now.utc.iso8601.gsub ":", "-"
"ruby-storage-control-folder-samples-test-#{t}-#{SecureRandom.hex 4}".downcase
end

def create_bucket_helper bucket_name, uniform_bucket_level_access: nil, hierarchical_namespace: nil
storage_client = Google::Cloud::Storage.new
retry_resource_exhaustion do
storage_client.create_bucket bucket_name
storage_client.create_bucket bucket_name do |b|
b.uniform_bucket_level_access = uniform_bucket_level_access
b.hierarchical_namespace = hierarchical_namespace
end
end
end

Expand Down
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
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__
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 google-cloud-storage-control/samples/storage_control_get_folder.rb
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__
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__
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__

0 comments on commit 2a71a7e

Please sign in to comment.