|
| 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 "google/cloud" |
| 20 | + |
| 21 | + gcloud = Google::Cloud.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 "google/cloud" |
| 36 | + |
| 37 | + gcloud = Google::Cloud.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 "google/cloud" |
| 51 | + |
| 52 | + gcloud = Google::Cloud.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