5
5
# --------------------------------------------------------------------------------------
6
6
7
7
8
+ """
9
+ A simple python wrapper for Google's `Firebase Cloud Storage REST API`_
10
+
11
+ .. _Firebase Cloud Storage REST API:
12
+ https://firebase.google.com/docs/reference/rest/storage/rest
13
+ """
14
+
8
15
import requests
9
16
from gcloud import storage
10
17
from urllib .parse import quote
13
20
14
21
15
22
class Storage :
16
- """ Storage Service """
23
+ """ Firebase Cloud Storage Service
24
+
25
+ :type credentials:
26
+ :class:`~oauth2client.service_account.ServiceAccountCredentials`
27
+ :param credentials: Service Account Credentials.
28
+
29
+ :type requests: :class:`~requests.Session`
30
+ :param requests: Session to make HTTP requests.
31
+
32
+ :type storage_bucket: str
33
+ :param storage_bucket: ``storageBucket`` from Firebase
34
+ configuration.
35
+ """
17
36
18
37
def __init__ (self , credentials , requests , storage_bucket ):
38
+ """ Constructor """
39
+
19
40
self .credentials = credentials
20
41
self .requests = requests
21
42
self .storage_bucket = "https://firebasestorage.googleapis.com/v0/b/" + storage_bucket
@@ -27,6 +48,17 @@ def __init__(self, credentials, requests, storage_bucket):
27
48
self .bucket = client .get_bucket (storage_bucket )
28
49
29
50
def child (self , * args ):
51
+ """ Build paths to your storage.
52
+
53
+
54
+ :type args: str
55
+ :param args: Positional arguments to build path to storage.
56
+
57
+
58
+ :return: A reference to the instance object.
59
+ :rtype: Storage
60
+ """
61
+
30
62
new_path = "/" .join (args )
31
63
32
64
if self .path :
@@ -40,6 +72,31 @@ def child(self, *args):
40
72
return self
41
73
42
74
def put (self , file , token = None ):
75
+ """ Upload file to storage.
76
+
77
+ | For more details:
78
+ | |upload_files|_
79
+
80
+ .. |upload_files| replace::
81
+ Firebase Documentation | Upload files with Cloud Storage on
82
+ Web
83
+
84
+ .. _upload_files:
85
+ https://firebase.google.com/docs/storage/web/upload-files#upload_files
86
+
87
+
88
+ :type file: str
89
+ :param file: Local path to file to upload.
90
+
91
+ :type token: str
92
+ :param token: (Optional) Firebase Auth User ID Token, defaults
93
+ to :data:`None`.
94
+
95
+
96
+ :return: Successful attempt returns :data:`None`.
97
+ :rtype: :data:`None`
98
+ """
99
+
43
100
# reset path
44
101
path = self .path
45
102
self .path = None
@@ -75,6 +132,26 @@ def put(self, file, token=None):
75
132
return request_object .json ()
76
133
77
134
def delete (self , name , token ):
135
+ """ Delete file from storage.
136
+
137
+ | For more details:
138
+ | |delete_a_file|_
139
+
140
+ .. |delete_a_file| replace::
141
+ Firebase Documentation | Delete files with Cloud Storage on
142
+ Web
143
+
144
+ .. _delete_a_file:
145
+ https://firebase.google.com/docs/storage/web/delete-files#delete_a_file
146
+
147
+
148
+ :type name: str
149
+ :param name: Cloud path to file.
150
+
151
+ :type token: str
152
+ :param token: Firebase Auth User ID Token
153
+ """
154
+
78
155
if self .credentials :
79
156
self .bucket .delete_blob (name )
80
157
else :
@@ -89,6 +166,30 @@ def delete(self, name, token):
89
166
raise_detailed_error (request_object )
90
167
91
168
def download (self , path , filename , token = None ):
169
+ """ Download file from storage.
170
+
171
+ | For more details:
172
+ | |download_data_via_url|_
173
+
174
+ .. |download_data_via_url| replace::
175
+ Firebase Documentation | Download files with Cloud Storage
176
+ on Web
177
+
178
+ .. _download_data_via_url:
179
+ https://firebase.google.com/docs/storage/web/download-files#download_data_via_url
180
+
181
+
182
+ :type path: str
183
+ :param path: Path to cloud file
184
+
185
+ :type filename: str
186
+ :param filename: File name to be downloaded as.
187
+
188
+ :type token: str
189
+ :param token: (Optional) Firebase Auth User ID Token, defaults
190
+ to :data:`None`.
191
+ """
192
+
92
193
# remove leading backlash
93
194
url = self .get_url (token )
94
195
@@ -118,6 +219,17 @@ def download(self, path, filename, token=None):
118
219
f .write (chunk )
119
220
120
221
def get_url (self , token ):
222
+ """ Fetches URL for file.
223
+
224
+
225
+ :type token: str
226
+ :param token: Firebase Auth User ID Token.
227
+
228
+
229
+ :return: URL for the file.
230
+ :rtype: str
231
+ """
232
+
121
233
path = self .path
122
234
self .path = None
123
235
@@ -130,4 +242,21 @@ def get_url(self, token):
130
242
return "{0}/o/{1}?alt=media" .format (self .storage_bucket , quote (path , safe = '' ))
131
243
132
244
def list_files (self ):
245
+ """ List of all files in storage.
246
+
247
+ | for more details:
248
+ | |list_all_files|_
249
+
250
+ .. |list_all_files| replace::
251
+ Firebase Documentation | List files with Cloud Storage on
252
+ Web
253
+
254
+ .. _list_all_files:
255
+ https://firebase.google.com/docs/storage/web/list-files#list_all_files
256
+
257
+
258
+ :return: list of :class:`~gcloud.storage.blob.Blob`
259
+ :rtype: :class:`~gcloud.storage.bucket._BlobIterator`
260
+ """
261
+
133
262
return self .bucket .list_blobs ()
0 commit comments