Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
bucket_operations.py
Go to the documentation of this file.
1from google.cloud import storage
2import os
3
4
6 """!
7 Set up the Google Cloud Storage client.
8 """
9 os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = (
10 "src/cloud_storage/evaluation-data-427214-926daf8b7126.json"
11 )
12 client = storage.Client()
13
14 return client
15
16
17def upload_csv_to_bucket(bucket_name, source_file_name, destination_blob_name):
18 """!
19 Upload a CSV file to a Google Cloud Storage bucket.
20
21 Args:
22 bucket_name: Name of the bucket to upload the file to.
23 source_file_name: Path to the file to upload.
24 destination_blob_name: Name of the file in the bucket.
25 """
26
27 client = setup_gcs_client()
28 bucket = client.bucket(bucket_name)
29 blob = bucket.blob(destination_blob_name)
30 blob.upload_from_filename(source_file_name)
31 print(f"File {source_file_name} uploaded to {destination_blob_name}.")
32
33
34def download_csv_from_bucket_to_folder(
35 bucket_name, source_blob_name, destination_folder, file_name
36):
37 """!
38 Download a CSV file from a Google Cloud Storage bucket to a specified folder.
39
40 Args:
41 bucket_name: Name of the bucket to download the file from.
42 source_blob_name: Name of the file in the bucket.
43 destination_folder: Folder to save the file to.
44 file_name: Name of the file to save.
45 """
46 client = setup_gcs_client()
47 bucket = client.bucket(bucket_name)
48 blob = bucket.blob(source_blob_name)
49
50 # Ensure the destination folder and any necessary subdirectories exist
51 destination_file_path = os.path.join(destination_folder, file_name)
52 os.makedirs(os.path.dirname(destination_file_path), exist_ok=True)
53
54 blob.download_to_filename(destination_file_path)
55 print(f"File {source_blob_name} downloaded to {destination_file_path}.")
56
57
58def list_blobs(bucket_name):
59 """!
60 List all files in a Google Cloud Storage bucket.
61 Args:
62 bucket_name: Name of the bucket to list files from.
63 """
64 client = setup_gcs_client()
65 blobs = client.list_blobs(bucket_name)
66
67 return [blob.name for blob in blobs]
setup_gcs_client()
Set up the Google Cloud Storage client.