Skip to content

Commit 4ccb96b

Browse files
author
remi Taylor
committed
Move Storage samples to executable buckets.rb and files.rb
1 parent 80a69b6 commit 4ccb96b

7 files changed

Lines changed: 347 additions & 306 deletions

File tree

storage/Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ source "https://rubygems.org"
1717
# Google Cloud Client Library for Ruby
1818
gem "gcloud"
1919

20-
gem "gli"
21-
gem "highline"
22-
2320
group :test do
2421
gem "rake"
2522
gem "rubocop"

storage/Gemfile.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ GEM
1414
grpc (= 1.0.0.pre1)
1515
mime-types (>= 2.4, < 4.0)
1616
zonefile (~> 1.04)
17-
gli (2.14.0)
1817
google-api-client (0.9.11)
1918
addressable (~> 2.3)
2019
googleauth (~> 0.5)
@@ -37,7 +36,6 @@ GEM
3736
grpc (1.0.0.pre1)
3837
google-protobuf (~> 3.0.0.alpha.5.0.3)
3938
googleauth (~> 0.5.1)
40-
highline (1.7.8)
4139
httpclient (2.8.2.2)
4240
hurley (0.2)
4341
jwt (1.5.4)
@@ -95,8 +93,6 @@ PLATFORMS
9593

9694
DEPENDENCIES
9795
gcloud
98-
gli
99-
highline
10096
rake
10197
rspec
10298
rubocop

storage/README.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1 @@
1-
# Google Cloud Storage Ruby sample
2-
3-
To run, first install dependencies
4-
5-
bundle install
6-
7-
Then run the `bundle exec buckets`
8-
9-
NAME
10-
buckets - Manage Google Cloud Storage buckets
11-
12-
SYNOPSIS
13-
buckets [global options] command [command options] [arguments...]
14-
15-
GLOBAL OPTIONS
16-
--help - Show this message
17-
-p, --project-id=PROJECT_ID - Your Google Cloud project ID (default: none)
18-
19-
COMMANDS
20-
create - Create a new bucket with the given name
21-
delete - Delete the specified bucket.
22-
help - Shows a list of commands or help for one command
23-
list - List all buckets in the authenticated project
1+
# Google Cloud Storage Ruby samples

storage/buckets

Lines changed: 0 additions & 74 deletions
This file was deleted.

storage/buckets.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in write, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def list_buckets project_id:
16+
# [START list_buckets]
17+
# project_id = "Your Google Cloud project ID"
18+
19+
require "gcloud"
20+
21+
gcloud = Gcloud.new project_id
22+
storage = gcloud.storage
23+
24+
storage.buckets.each do |bucket|
25+
puts bucket.name
26+
end
27+
# [END list_buckets]
28+
end
29+
30+
def create_bucket project_id:, bucket_name:
31+
# [START create_bucket]
32+
# project_id = "Your Google Cloud project ID"
33+
# bucket_name = "Name of Google Cloud Storage bucket to create"
34+
35+
require "gcloud"
36+
37+
gcloud = Gcloud.new project_id
38+
storage = gcloud.storage
39+
bucket = storage.create_bucket bucket_name
40+
41+
puts "Created bucket: #{bucket.name}"
42+
# [END create_bucket]
43+
end
44+
45+
def delete_bucket project_id:, bucket_name:
46+
# [START delete_bucket]
47+
# project_id = "Your Google Cloud project ID"
48+
# bucket_name = "Name of your Google Cloud Storage bucket to delete"
49+
50+
require "gcloud"
51+
52+
gcloud = Gcloud.new project_id
53+
storage = gcloud.storage
54+
bucket = storage.bucket bucket_name
55+
56+
bucket.delete
57+
58+
puts "Deleted bucket: #{bucket.name}"
59+
# [END delete_bucket]
60+
end
61+
62+
if __FILE__ == $0
63+
case ARGV.shift
64+
when "list"
65+
list_buckets project_id: ENV["GCLOUD_PROJECT"]
66+
when "create"
67+
create_bucket project_id: ENV["GCLOUD_PROJECT"],
68+
bucket_name: ARGV.shift
69+
when "delete"
70+
delete_bucket project_id: ENV["GCLOUD_PROJECT"],
71+
bucket_name: ARGV.shift
72+
else
73+
puts <<-usage
74+
Usage: bundle exec ruby buckets.rb [command] [arguments]
75+
76+
Commands:
77+
list List all buckets in the authenticated project
78+
create <bucket> Create a new bucket with the provided name
79+
delete <bucket> Delete bucket with the provided name
80+
81+
Environment variables:
82+
GCLOUD_PROJECT must be set to your Google Cloud project ID
83+
usage
84+
end
85+
end

0 commit comments

Comments
 (0)