Skip to content

Commit c312fc5

Browse files
author
remi Taylor
authored
Merge pull request #39 from GoogleCloudPlatform/new-storage-samples
Add Google Cloud Storage samples
2 parents 678f2f9 + d56b912 commit c312fc5

File tree

10 files changed

+647
-48
lines changed

10 files changed

+647
-48
lines changed

storage/Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015 Google, Inc
1+
# Copyright 2016 Google, Inc
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414

1515
source "https://rubygems.org"
1616

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

1919
group :test do
2020
gem "rspec"

storage/Gemfile.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ GEM
33
specs:
44
addressable (2.4.0)
55
diff-lcs (1.2.5)
6+
digest-crc (0.4.1)
67
faraday (0.9.2)
78
multipart-post (>= 1.2, < 3)
89
google-api-client (0.9.15)
@@ -14,6 +15,11 @@ GEM
1415
mime-types (>= 1.6)
1516
representable (~> 2.3.0)
1617
retriable (~> 2.0)
18+
google-cloud-core (0.20.1)
19+
google-cloud-storage (0.20.1)
20+
digest-crc (~> 0.4)
21+
google-api-client (~> 0.9.11)
22+
google-cloud-core (~> 0.20.0)
1723
googleauth (0.5.1)
1824
faraday (~> 0.9)
1925
jwt (~> 1.4)
@@ -63,7 +69,7 @@ PLATFORMS
6369
ruby
6470

6571
DEPENDENCIES
66-
google-api-client (~> 0.9.pre3)
72+
google-cloud-storage
6773
rspec
6874

6975
BUNDLED WITH

storage/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<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"/>
2+
3+
# Google Cloud Storage Ruby Samples
4+
5+
[Cloud Storage][storage_docs] allows world-wide storage and retrieval of any
6+
amount of data at any time.
7+
8+
[storage_docs]: https://cloud.google.com/storage/docs/
9+
10+
## Run sample
11+
12+
To run the sample, first install dependencies:
13+
14+
bundle install
15+
16+
Run the sample:
17+
18+
bundle exec ruby buckets.rb
19+
bundle exec ruby files.rb
20+
21+
## Samples
22+
23+
### Buckets
24+
25+
**Usage:** `bundle exec ruby buckets.rb [command] [arguments]`
26+
27+
```
28+
sage: bundle exec ruby buckets.rb [command] [arguments]
29+
30+
Commands:
31+
list List all buckets in the authenticated project
32+
create <bucket> Create a new bucket with the provided name
33+
delete <bucket> Delete bucket with the provided name
34+
35+
Environment variables:
36+
GCLOUD_PROJECT must be set to your Google Cloud project ID
37+
```
38+
39+
### Files
40+
41+
**Usage:** `bundle exec ruby files.rb [command] [arguments]`
42+
43+
```
44+
Usage: bundle exec ruby files.rb [command] [arguments]
45+
46+
Commands:
47+
list <bucket> List all files in the bucket
48+
upload <bucket> <file> Upload local file to a bucket
49+
download <bucket> <file> <path> Download a file from a bucket
50+
delete <bucket> <file> Delete a file from a bucket
51+
metadata <bucket> <file> Display metadata for a file in a bucket
52+
make_public <bucket> <file> Make a file in a bucket public
53+
rename <bucket> <file> <new> Rename a file in a bucket
54+
copy <srcBucket> <srcFile> <destBucket> <destFile> Copy file to other bucket
55+
56+
Environment variables:
57+
GCLOUD_PROJECT must be set to your Google Cloud project ID
58+
```

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 "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

Comments
 (0)