42
42
import os
43
43
import time
44
44
45
+ from google .cloud import pubsub
46
+ from google .cloud import storage
45
47
import jwt
46
48
import requests as req
47
49
@@ -127,18 +129,16 @@ def publish_pubsub_message(
127
129
rsa_private_key_path ,
128
130
)
129
131
130
- # Create Pub/Sub topic
131
- request_path = "https://pubsub.googleapis.com/v1/projects/{}/topics/{}" .format (
132
- project_id , topic_id
133
- )
132
+ pubsub_client = pubsub .PublisherClient ()
133
+ topic_path = pubsub_client .topic_path (project_id , topic_id )
134
+ pubsub_client .create_topic (request = {"name" : topic_path })
135
+ print ("Successfully created Pub/Sub topic: {}." .format (topic_id ))
136
+
134
137
headers = {
135
138
"Authorization" : "Bearer {}" .format (access_token ),
136
139
"content-type" : "application/json" ,
137
140
"cache-control" : "no-cache" ,
138
141
}
139
- resp = req .put (url = request_path , data = {}, headers = headers )
140
- assert resp .ok , resp .raise_for_status ()
141
- print ("Successfully created Pub/Sub topic: {}." .format (topic_id ))
142
142
143
143
# Publish message to Pub/Sub topic
144
144
publish_payload = {
@@ -160,11 +160,7 @@ def publish_pubsub_message(
160
160
)
161
161
162
162
# Delete Pub/Sub topic
163
- pubsub_delete_request_path = "https://pubsub.googleapis.com/v1/projects/{}/topics/{}" .format (
164
- project_id , topic_id
165
- )
166
- delete_resp = req .delete (url = pubsub_delete_request_path , headers = headers )
167
- assert delete_resp .ok , delete_resp .raise_for_status ()
163
+ pubsub_client .delete_topic (request = {"topic" : topic_path })
168
164
print ("Successfully deleted Pub/Sub topic: {}" .format (topic_id ))
169
165
# [END iot_access_token_pubsub]
170
166
@@ -202,27 +198,18 @@ def download_cloud_storage_file(
202
198
rsa_private_key_path ,
203
199
)
204
200
205
- # Create GCS bucket
206
- create_payload = {
207
- "name" : bucket_name ,
208
- "location" : cloud_region ,
209
- "storageClass" : "STANDARD" ,
210
- "iamConfiguration" : {"uniformBucketLevelAccess" : {"enabled" : True }},
211
- }
212
- create_request_path = "https://storage.googleapis.com/storage/v1/b?project={}" .format (
213
- project_id
214
- )
215
201
headers = {
216
202
"authorization" : "Bearer {}" .format (access_token ),
217
203
"content-type" : "application/json" ,
218
204
"cache-control" : "no-cache" ,
219
205
}
220
- create_resp = req .post (
221
- url = create_request_path ,
222
- data = bytes (json .dumps (create_payload ), "utf-8" ),
223
- headers = headers ,
224
- )
225
- assert create_resp .ok , create_resp .raise_for_status ()
206
+
207
+ # Create GCS bucket
208
+ storage_client = storage .Client ()
209
+ bucket = storage_client .bucket (bucket_name )
210
+ bucket .storage_class = "COLDLINE"
211
+ bucket .iam_configuration .uniform_bucket_level_access_enabled = True
212
+ storage_client .create_bucket (bucket , location = cloud_region )
226
213
print ("Successfully created Storage bucket: {}" .format (bucket_name ))
227
214
228
215
# Upload data to GCS bucket.
@@ -232,6 +219,7 @@ def download_cloud_storage_file(
232
219
bucket_name , data_name
233
220
)
234
221
upload_resp = req .post (url = upload_request_path , data = binary_data , headers = headers )
222
+ print ("Upload response: " , upload_resp .json ())
235
223
assert upload_resp .ok , upload_resp .raise_for_status ()
236
224
print (
237
225
"Successfully uploaded {} as {} to bucket {}." .format (
@@ -248,19 +236,12 @@ def download_cloud_storage_file(
248
236
print ("Successfully downloaded {} from bucket {}." .format (data_name , bucket_name ))
249
237
250
238
# Delete data from GCS bucket.
251
- delete_request_path = "https://storage.googleapis.com/storage/v1/b/{}/o/{}" .format (
252
- bucket_name , data_name
253
- )
254
- delete_data_resp = req .delete (url = delete_request_path , headers = headers )
255
- assert delete_data_resp .ok , delete_data_resp .raise_for_status ()
239
+ blob = bucket .blob (data_name )
240
+ blob .delete ()
256
241
print ("Successfully deleted {} from bucket {}." .format (data_name , bucket_name ))
257
242
258
243
# Delete GCS Bucket
259
- gcs_delete_request_path = "https://storage.googleapis.com/storage/v1/b/{}" .format (
260
- bucket_name
261
- )
262
- delete_resp = req .delete (url = gcs_delete_request_path , headers = headers )
263
- assert delete_resp .ok , delete_resp .raise_for_status ()
244
+ bucket .delete ()
264
245
print ("Successfully deleted bucket: {}" .format (bucket_name ))
265
246
# [END iot_access_token_gcs]
266
247
0 commit comments