Skip to content

Commit 200fc67

Browse files
committed
GCP
1 parent 0da5c2c commit 200fc67

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# import packages
2+
from google.cloud import storage
3+
import os
4+
5+
# set key credentials file path
6+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/cloudquicklabs-93d1e8c6ac6a.json'
7+
8+
# define function that creates the bucket
9+
def create_bucket(bucket_name, storage_class='STANDARD', location='us-central1'):
10+
storage_client = storage.Client()
11+
12+
bucket = storage_client.bucket(bucket_name)
13+
bucket.storage_class = storage_class
14+
15+
bucket = storage_client.create_bucket(bucket, location=location)
16+
# for dual-location buckets add data_locations=[region_1, region_2]
17+
18+
return f'Bucket {bucket.name} successfully created.'
19+
20+
## Invoke Function
21+
print(create_bucket('test_demo_storage_bucket', 'STANDARD', 'us-central1'))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# import packages
2+
from google.cloud import storage
3+
import os
4+
5+
# set key credentials file path
6+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/cloudquicklabs-93d1e8c6ac6a.json'
7+
8+
# define function that downloads a file from the bucket
9+
def download_cs_file(bucket_name, file_name, destination_file_name):
10+
storage_client = storage.Client()
11+
12+
bucket = storage_client.bucket(bucket_name)
13+
14+
blob = bucket.blob(file_name)
15+
blob.download_to_filename(destination_file_name)
16+
17+
return True
18+
19+
download_cs_file('test_demo_storage_bucket', 'json/test.json', 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/download.json')
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# import packages
2+
from google.cloud import storage
3+
import os
4+
5+
# set key credentials file path
6+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/cloudquicklabs-93d1e8c6ac6a.json'
7+
8+
# define function that list files in the bucket
9+
def list_cs_files(bucket_name):
10+
storage_client = storage.Client()
11+
12+
file_list = storage_client.list_blobs(bucket_name)
13+
file_list = [file.name for file in file_list]
14+
15+
return file_list
16+
17+
print(list_cs_files('test_demo_storage_bucket'))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# import packages
2+
from google.cloud import storage
3+
import os
4+
5+
# set key credentials file path
6+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/cloudquicklabs-93d1e8c6ac6a.json'
7+
8+
# define function that uploads a file from the bucket
9+
def upload_cs_file(bucket_name, source_file_name, destination_file_name):
10+
storage_client = storage.Client()
11+
12+
bucket = storage_client.bucket(bucket_name)
13+
14+
blob = bucket.blob(destination_file_name)
15+
blob.upload_from_filename(source_file_name)
16+
17+
return True
18+
19+
upload_cs_file('test_demo_storage_bucket', 'D:/VSCode/GitRepos/PythonHacks/GCP_Storage_Bucket_Handling_With_Python/cloudquicklabs-93d1e8c6ac6a.json', 'json/test.json')

0 commit comments

Comments
 (0)