-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
40 lines (32 loc) · 1.1 KB
/
storage.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
import datetime
from google.cloud import storage
from werkzeug import secure_filename
client = storage.Client(project='cloudcomputingcompiler')
bucket = client.bucket('cloudcomputingcompilercodecontainer')
def upload_file(file, safe_filename):
blob = bucket.blob(safe_filename)
blob.upload_from_file(file)
url = blob.public_url
return url
def upload_string(string, safe_filename):
blob = bucket.blob(safe_filename)
blob.upload_from_string(string)
url = blob.public_url
return url
def download_file(filename, file):
blob = bucket.blob(filename)
blob.download_to_file(file)
def download_string(filename):
blob = bucket.blob(filename)
return blob.download_as_string()
def delete_file(filename):
blob = bucket.blob(filename)
blob.delete()
def file_exists(filename):
blob = bucket.blob(filename)
return blob.exists()
def safe_filename(filename):
filename = secure_filename(filename)
date = datetime.datetime.utcnow().strftime("%Y-%m-%d-%H%M%S")
basename, extension = filename.rsplit('.', 1)
return "{0}-{1}.{2}".format(basename, date, extension)