Skip to content

Commit

Permalink
Merge pull request #39 from GoogleCloudPlatform/new-storage-samples
Browse files Browse the repository at this point in the history
Add Google Cloud Storage samples
  • Loading branch information
remi Taylor authored Sep 29, 2016
2 parents 678f2f9 + d56b912 commit c312fc5
Show file tree
Hide file tree
Showing 10 changed files with 647 additions and 48 deletions.
4 changes: 2 additions & 2 deletions storage/Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 Google, Inc
# Copyright 2016 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@

source "https://rubygems.org"

gem "google-api-client", "~>0.9.pre3"
gem "google-cloud-storage"

group :test do
gem "rspec"
Expand Down
8 changes: 7 additions & 1 deletion storage/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ GEM
specs:
addressable (2.4.0)
diff-lcs (1.2.5)
digest-crc (0.4.1)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
google-api-client (0.9.15)
Expand All @@ -14,6 +15,11 @@ GEM
mime-types (>= 1.6)
representable (~> 2.3.0)
retriable (~> 2.0)
google-cloud-core (0.20.1)
google-cloud-storage (0.20.1)
digest-crc (~> 0.4)
google-api-client (~> 0.9.11)
google-cloud-core (~> 0.20.0)
googleauth (0.5.1)
faraday (~> 0.9)
jwt (~> 1.4)
Expand Down Expand Up @@ -63,7 +69,7 @@ PLATFORMS
ruby

DEPENDENCIES
google-api-client (~> 0.9.pre3)
google-cloud-storage
rspec

BUNDLED WITH
Expand Down
58 changes: 58 additions & 0 deletions storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Google Cloud Storage Ruby Samples

[Cloud Storage][storage_docs] allows world-wide storage and retrieval of any
amount of data at any time.

[storage_docs]: https://cloud.google.com/storage/docs/

## Run sample

To run the sample, first install dependencies:

bundle install

Run the sample:

bundle exec ruby buckets.rb
bundle exec ruby files.rb

## Samples

### Buckets

**Usage:** `bundle exec ruby buckets.rb [command] [arguments]`

```
sage: bundle exec ruby buckets.rb [command] [arguments]
Commands:
list List all buckets in the authenticated project
create <bucket> Create a new bucket with the provided name
delete <bucket> Delete bucket with the provided name
Environment variables:
GCLOUD_PROJECT must be set to your Google Cloud project ID
```

### Files

**Usage:** `bundle exec ruby files.rb [command] [arguments]`

```
Usage: bundle exec ruby files.rb [command] [arguments]
Commands:
list <bucket> List all files in the bucket
upload <bucket> <file> Upload local file to a bucket
download <bucket> <file> <path> Download a file from a bucket
delete <bucket> <file> Delete a file from a bucket
metadata <bucket> <file> Display metadata for a file in a bucket
make_public <bucket> <file> Make a file in a bucket public
rename <bucket> <file> <new> Rename a file in a bucket
copy <srcBucket> <srcFile> <destBucket> <destFile> Copy file to other bucket
Environment variables:
GCLOUD_PROJECT must be set to your Google Cloud project ID
```
85 changes: 85 additions & 0 deletions storage/buckets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2016 Google, Inc
#
# 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 write, 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.

def list_buckets project_id:
# [START list_buckets]
# project_id = "Your Google Cloud project ID"

require "google/cloud"

gcloud = Google::Cloud.new project_id
storage = gcloud.storage

storage.buckets.each do |bucket|
puts bucket.name
end
# [END list_buckets]
end

def create_bucket project_id:, bucket_name:
# [START create_bucket]
# project_id = "Your Google Cloud project ID"
# bucket_name = "Name of Google Cloud Storage bucket to create"

require "google/cloud"

gcloud = Google::Cloud.new project_id
storage = gcloud.storage
bucket = storage.create_bucket bucket_name

puts "Created bucket: #{bucket.name}"
# [END create_bucket]
end

def delete_bucket project_id:, bucket_name:
# [START delete_bucket]
# project_id = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket to delete"

require "google/cloud"

gcloud = Google::Cloud.new project_id
storage = gcloud.storage
bucket = storage.bucket bucket_name

bucket.delete

puts "Deleted bucket: #{bucket.name}"
# [END delete_bucket]
end

if __FILE__ == $0
case ARGV.shift
when "list"
list_buckets project_id: ENV["GCLOUD_PROJECT"]
when "create"
create_bucket project_id: ENV["GCLOUD_PROJECT"],
bucket_name: ARGV.shift
when "delete"
delete_bucket project_id: ENV["GCLOUD_PROJECT"],
bucket_name: ARGV.shift
else
puts <<-usage
Usage: bundle exec ruby buckets.rb [command] [arguments]
Commands:
list List all buckets in the authenticated project
create <bucket> Create a new bucket with the provided name
delete <bucket> Delete bucket with the provided name
Environment variables:
GCLOUD_PROJECT must be set to your Google Cloud project ID
usage
end
end
Loading

0 comments on commit c312fc5

Please sign in to comment.