-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageCloud.py
74 lines (50 loc) · 2.17 KB
/
ImageCloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import base64
import datetime
import os
from google.cloud import storage
def save_image_to_gcs(base64_image):
# Get the current time
current_time = datetime.datetime.now().time()
# Format the time as HH:MM:SS
time_string = current_time.strftime("%H_%M_%S")
print("Current time string:", time_string)
# Decode the base64 image
image_data = base64.b64decode(base64_image)
# Set up Google Cloud Storage client
json_key_path = 'keys.json' # Path to your JSON key file
storage_client = storage.Client.from_service_account_json(json_key_path)
# Set the bucket and filename for the image
bucket_name = 'restbaitempo'
filename = time_string+".png"
# Upload the image to the specified bucket
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(filename)
blob.upload_from_string(image_data, content_type='image/png')
# Generate the public URL for the image
url = f'https://storage.googleapis.com/{bucket_name}/{filename}'
return url, 640, 480
def save_png_to_gcs(file_path):
# Get the current time
current_time = datetime.datetime.now().time()
# Format the time as HH:MM:SS
time_string = current_time.strftime("%H_%M_%S")
print("Current time string:", time_string)
# Set up Google Cloud Storage client
json_key_path = 'keys.json' # Path to your JSON key file
storage_client = storage.Client.from_service_account_json(json_key_path)
# Set the bucket and filename for the image
bucket_name = 'restbaitempo'
filename = time_string + ".png"
# Upload the image to the specified bucket
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(filename)
blob.upload_from_filename(file_path, content_type='image/png')
# Generate the public URL for the image
url = f'https://storage.googleapis.com/{bucket_name}/{filename}'
return url, 640, 480
sample_base64_image = ""
if __name__ == "__main__":
image_url = save_image_to_gcs(sample_base64_image)
image_url = save_png_to_gcs('C:\\Users\\daapesteguiat\\Downloads\\flatfurni.png')
# Print the URL of the uploaded image
print("Image uploaded successfully. URL:", image_url)