55# --------------------------------------------------------------------------------------
66
77
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+
815import requests
916from gcloud import storage
1017from urllib .parse import quote
1320
1421
1522class 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+ """
1736
1837 def __init__ (self , credentials , requests , storage_bucket ):
38+ """ Constructor """
39+
1940 self .credentials = credentials
2041 self .requests = requests
2142 self .storage_bucket = "https://firebasestorage.googleapis.com/v0/b/" + storage_bucket
@@ -27,6 +48,17 @@ def __init__(self, credentials, requests, storage_bucket):
2748 self .bucket = client .get_bucket (storage_bucket )
2849
2950 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+
3062 new_path = "/" .join (args )
3163
3264 if self .path :
@@ -40,6 +72,31 @@ def child(self, *args):
4072 return self
4173
4274 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+
43100 # reset path
44101 path = self .path
45102 self .path = None
@@ -75,6 +132,26 @@ def put(self, file, token=None):
75132 return request_object .json ()
76133
77134 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+
78155 if self .credentials :
79156 self .bucket .delete_blob (name )
80157 else :
@@ -89,6 +166,30 @@ def delete(self, name, token):
89166 raise_detailed_error (request_object )
90167
91168 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+
92193 # remove leading backlash
93194 url = self .get_url (token )
94195
@@ -118,6 +219,17 @@ def download(self, path, filename, token=None):
118219 f .write (chunk )
119220
120221 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+
121233 path = self .path
122234 self .path = None
123235
@@ -130,4 +242,21 @@ def get_url(self, token):
130242 return "{0}/o/{1}?alt=media" .format (self .storage_bucket , quote (path , safe = '' ))
131243
132244 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+
133262 return self .bucket .list_blobs ()
0 commit comments