1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- # TODO use consistent wording : "object" | "blob" | file"
16-
1715def list_bucket_contents project_id :, bucket_name :
1816 # [START list_bucket_contents]
1917 # project_id = "Your Google Cloud project ID"
2018 # bucket_name = "Your Google Cloud Storage bucket name"
2119
22- require "gcloud "
20+ require "google/cloud "
2321
24- gcloud = Gcloud . new project_id
22+ gcloud = Google :: Cloud . new project_id
2523 storage = gcloud . storage
2624 bucket = storage . bucket bucket_name
2725
@@ -31,75 +29,93 @@ def list_bucket_contents project_id:, bucket_name:
3129 # [END list_bucket_contents]
3230end
3331
34- # TODO
35- # def list_bucket_contents_with_prefix
36-
37- def upload_object project_id :, bucket_name :, local_path :
38- # [START upload_object]
32+ def list_bucket_contents_with_prefix project_id :, bucket_name :, prefix :
33+ # [START list_bucket_contents]
3934 # project_id = "Your Google Cloud project ID"
4035 # bucket_name = "Your Google Cloud Storage bucket name"
41- # local_path = "Path to local file to upload "
36+ # prefix = "Filter results to files whose names begin with this prefix "
4237
43- require "gcloud "
38+ require "google/cloud "
4439
45- gcloud = Gcloud . new project_id
40+ gcloud = Google :: Cloud . new project_id
4641 storage = gcloud . storage
4742 bucket = storage . bucket bucket_name
43+ files = bucket . files prefix : prefix
4844
49- file = bucket . create_file local_path
45+ files . each do |file |
46+ puts file . name
47+ end
48+ # [END list_bucket_contents]
49+ end
50+
51+ def upload_file project_id :, bucket_name :, local_file_path :,
52+ storage_file_path :
53+ # [START upload_file]
54+ # project_id = "Your Google Cloud project ID"
55+ # bucket_name = "Your Google Cloud Storage bucket name"
56+ # local_file_path = "Path to local file to upload"
57+ # storage_file_path = "Path to store the file in Google Cloud Storage"
58+
59+ require "google/cloud"
60+
61+ gcloud = Google ::Cloud . new project_id
62+ storage = gcloud . storage
63+ bucket = storage . bucket bucket_name
64+
65+ file = bucket . create_file local_file_path , storage_file_path
5066
5167 puts "Uploaded #{ file . name } "
52- # [END upload_object ]
68+ # [END upload_file ]
5369end
5470
55- def download_object project_id :, bucket_name :, file_name :, local_path :
56- # [START download_object ]
71+ def download_file project_id :, bucket_name :, file_name :, local_path :
72+ # [START download_file ]
5773 # project_id = "Your Google Cloud project ID"
5874 # bucket_name = "Your Google Cloud Storage bucket name"
5975 # file_name = "Name of file in Google Cloud Storage to download locally"
6076 # local_path = "Path to local file to save"
6177
62- require "gcloud "
78+ require "google/cloud "
6379
64- gcloud = Gcloud . new project_id
80+ gcloud = Google :: Cloud . new project_id
6581 storage = gcloud . storage
6682 bucket = storage . bucket bucket_name
6783 file = bucket . file file_name
6884
6985 file . download local_path
7086
7187 puts "Downloaded #{ file . name } "
72- # [END download_object ]
88+ # [END download_file ]
7389end
7490
75- def delete_object project_id :, bucket_name :, file_name :
76- # [START delete_object ]
91+ def delete_file project_id :, bucket_name :, file_name :
92+ # [START delete_file ]
7793 # project_id = "Your Google Cloud project ID"
7894 # bucket_name = "Your Google Cloud Storage bucket name"
7995 # file_name = "Name of file in Google Cloud Storage to delete"
8096
81- require "gcloud "
97+ require "google/cloud "
8298
83- gcloud = Gcloud . new project_id
99+ gcloud = Google :: Cloud . new project_id
84100 storage = gcloud . storage
85101 bucket = storage . bucket bucket_name
86102 file = bucket . file file_name
87103
88104 file . delete
89105
90106 puts "Deleted #{ file . name } "
91- # [END delete_object ]
107+ # [END delete_file ]
92108end
93109
94- def list_object_details project_id :, bucket_name :, file_name :
95- # [START list_object_details ]
110+ def list_file_details project_id :, bucket_name :, file_name :
111+ # [START list_file_details ]
96112 # project_id = "Your Google Cloud project ID"
97113 # bucket_name = "Your Google Cloud Storage bucket name"
98114 # file_name = "Name of file in Google Cloud Storage"
99115
100- require "gcloud "
116+ require "google/cloud "
101117
102- gcloud = Gcloud . new project_id
118+ gcloud = Google :: Cloud . new project_id
103119 storage = gcloud . storage
104120 bucket = storage . bucket bucket_name
105121 file = bucket . file file_name
@@ -126,41 +142,41 @@ def list_object_details project_id:, bucket_name:, file_name:
126142 file . metadata . each do |key , value |
127143 puts " - #{ key } = #{ value } "
128144 end
129- # [END list_object_details ]
145+ # [END list_file_details ]
130146end
131147
132- def make_object_public project_id :, bucket_name :, file_name :
133- # [START make_object_public ]
148+ def make_file_public project_id :, bucket_name :, file_name :
149+ # [START make_file_public ]
134150 # project_id = "Your Google Cloud project ID"
135151 # bucket_name = "Your Google Cloud Storage bucket name"
136152 # file_name = "Name of file in Google Cloud Storage to make public"
137153
138- require "gcloud "
154+ require "google/cloud "
139155
140- gcloud = Gcloud . new project_id
156+ gcloud = Google :: Cloud . new project_id
141157 storage = gcloud . storage
142158 bucket = storage . bucket bucket_name
143159 file = bucket . file file_name
144160
145161 file . acl . public!
146162
147163 puts "#{ file . name } is publicly accessible at #{ file . public_url } "
148- # [END make_object_public ]
164+ # [END make_file_public ]
149165end
150166
151167# TODO
152168# def generate_signed_url
153169
154- def rename_object project_id :, bucket_name :, file_name :, new_name :
155- # [START rename_object ]
170+ def rename_file project_id :, bucket_name :, file_name :, new_name :
171+ # [START rename_file ]
156172 # project_id = "Your Google Cloud project ID"
157173 # bucket_name = "Your Google Cloud Storage bucket name"
158174 # file_name = "Name of file in Google Cloud Storage to rename"
159175 # new_name = "File will be renamed to this new name"
160176
161- require "gcloud "
177+ require "google/cloud "
162178
163- gcloud = Gcloud . new project_id
179+ gcloud = Google :: Cloud . new project_id
164180 storage = gcloud . storage
165181 bucket = storage . bucket bucket_name
166182 file = bucket . file file_name
@@ -170,21 +186,21 @@ def rename_object project_id:, bucket_name:, file_name:, new_name:
170186 file . delete
171187
172188 puts "my-file.txt has been renamed to #{ renamed_file . name } "
173- # [END rename_object ]
189+ # [END rename_file ]
174190end
175191
176- def copy_object project_id :, source_bucket_name :, source_file_name :,
192+ def copy_file project_id :, source_bucket_name :, source_file_name :,
177193 dest_bucket_name :, dest_file_name :
178- # [START copy_object ]
194+ # [START copy_file ]
179195 # project_id = "Your Google Cloud project ID"
180196 # source_bucket_name = "Source bucket to copy file from"
181197 # source_file_name = "Source file name"
182198 # dest_bucket_name = "Destination bucket to copy file to"
183199 # dest_file_name = "Destination file name"
184200
185- require "gcloud "
201+ require "google/cloud "
186202
187- gcloud = Gcloud . new project_id
203+ gcloud = Google :: Cloud . new project_id
188204 storage = gcloud . storage
189205 bucket = storage . bucket source_bucket_name
190206 file = bucket . file source_file_name
@@ -194,7 +210,7 @@ def copy_object project_id:, source_bucket_name:, source_file_name:,
194210
195211 puts "#{ file . name } in #{ bucket . name } copied to " +
196212 "#{ copied_file . name } in #{ destination_bucket . name } "
197- # [END copy_object ]
213+ # [END copy_file ]
198214end
199215
200216def run_sample arguments
@@ -207,35 +223,35 @@ def run_sample arguments
207223 when "list_prefix"
208224 raise NotImplementedError , "list_prefix"
209225 when "upload"
210- upload_object project_id : ENV [ "GCLOUD_PROJECT" ] ,
211- bucket_name : arguments . shift ,
212- local_path : arguments . shift
226+ upload_file project_id : ENV [ "GCLOUD_PROJECT" ] ,
227+ bucket_name : arguments . shift ,
228+ local_file_path : arguments . shift
213229 when "download"
214- download_object project_id : ENV [ "GCLOUD_PROJECT" ] ,
230+ download_file project_id : ENV [ "GCLOUD_PROJECT" ] ,
215231 bucket_name : arguments . shift ,
216232 file_name : arguments . shift ,
217233 local_path : arguments . shift
218234 when "delete"
219- delete_object project_id : ENV [ "GCLOUD_PROJECT" ] ,
235+ delete_file project_id : ENV [ "GCLOUD_PROJECT" ] ,
220236 bucket_name : arguments . shift ,
221237 file_name : arguments . shift
222238 when "metadata"
223- list_object_details project_id : ENV [ "GCLOUD_PROJECT" ] ,
239+ list_file_details project_id : ENV [ "GCLOUD_PROJECT" ] ,
224240 bucket_name : arguments . shift ,
225241 file_name : arguments . shift
226242 when "make_public"
227- make_object_public project_id : ENV [ "GCLOUD_PROJECT" ] ,
243+ make_file_public project_id : ENV [ "GCLOUD_PROJECT" ] ,
228244 bucket_name : arguments . shift ,
229245 file_name : arguments . shift
230246 when "signed_url"
231247 raise NotImplementedError , "signed_url"
232248 when "rename"
233- rename_object project_id : ENV [ "GCLOUD_PROJECT" ] ,
249+ rename_file project_id : ENV [ "GCLOUD_PROJECT" ] ,
234250 bucket_name : arguments . shift ,
235251 file_name : arguments . shift ,
236252 new_name : arguments . shift
237253 when "copy"
238- copy_object project_id : ENV [ "GCLOUD_PROJECT" ] ,
254+ copy_file project_id : ENV [ "GCLOUD_PROJECT" ] ,
239255 source_bucket_name : arguments . shift ,
240256 source_file_name : arguments . shift ,
241257 dest_bucket_name : arguments . shift ,
0 commit comments