14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- # [START sample ]
17
+ # [START gae_storage_sample ]
18
18
"""A sample app that uses GCS client to operate on bucket and file."""
19
19
20
- # [START imports ]
20
+ # [START gae_storage_imports ]
21
21
import os
22
22
23
23
import cloudstorage
24
24
from google .appengine .api import app_identity
25
25
26
26
import webapp2
27
+ # [END gae_storage_imports]
27
28
28
- # [END imports]
29
-
30
- # [START retries]
31
29
cloudstorage .set_default_retry_params (
32
30
cloudstorage .RetryParams (
33
31
initial_delay = 0.2 , max_delay = 5.0 , backoff_factor = 2 , max_retry_period = 15
34
32
)
35
33
)
36
- # [END retries]
37
34
38
35
39
36
class MainPage (webapp2 .RequestHandler ):
40
37
"""Main page for GCS demo application."""
41
38
42
- # [START get_default_bucket ]
39
+ # [START gae_storage_get_default_bucket ]
43
40
def get (self ):
44
41
bucket_name = os .environ .get (
45
42
"BUCKET_NAME" , app_identity .get_default_gcs_bucket_name ()
@@ -52,7 +49,7 @@ def get(self):
52
49
)
53
50
)
54
51
self .response .write ("Using bucket name: {}\n \n " .format (bucket_name ))
55
- # [END get_default_bucket ]
52
+ # [END gae_storage_get_default_bucket ]
56
53
57
54
bucket = "/" + bucket_name
58
55
filename = bucket + "/demo-testfile"
@@ -79,7 +76,7 @@ def get(self):
79
76
self .delete_files ()
80
77
self .response .write ("\n \n The demo ran successfully!\n " )
81
78
82
- # [START write ]
79
+ # [START gae_storage_write ]
83
80
def create_file (self , filename ):
84
81
"""Create a file."""
85
82
@@ -98,19 +95,17 @@ def create_file(self, filename):
98
95
cloudstorage_file .write ("abcde\n " )
99
96
cloudstorage_file .write ("f" * 1024 * 4 + "\n " )
100
97
self .tmp_filenames_to_clean_up .append (filename )
98
+ # [END gae_storage_write]
101
99
102
- # [END write]
103
-
104
- # [START read]
100
+ # [START gae_storage_read]
105
101
def read_file (self , filename ):
106
102
self .response .write ("Abbreviated file content (first line and last 1K):\n " )
107
103
108
104
with cloudstorage .open (filename ) as cloudstorage_file :
109
105
self .response .write (cloudstorage_file .readline ())
110
106
cloudstorage_file .seek (- 1024 , os .SEEK_END )
111
107
self .response .write (cloudstorage_file .read ())
112
-
113
- # [END read]
108
+ # [END gae_storage_read]
114
109
115
110
def stat_file (self , filename ):
116
111
self .response .write ("File stat:\n " )
@@ -126,7 +121,7 @@ def create_files_for_list_bucket(self, bucket):
126
121
for f in filenames :
127
122
self .create_file (f )
128
123
129
- # [START list_bucket ]
124
+ # [START gae_storage_list_bucket ]
130
125
def list_bucket (self , bucket ):
131
126
"""Create several files and paginate through them."""
132
127
@@ -147,8 +142,7 @@ def list_bucket(self, bucket):
147
142
stats = cloudstorage .listbucket (
148
143
bucket + "/foo" , max_keys = page_size , marker = stat .filename
149
144
)
150
-
151
- # [END list_bucket]
145
+ # [END gae_storage_list_bucket]
152
146
153
147
def list_bucket_directory_mode (self , bucket ):
154
148
self .response .write ("Listbucket directory mode result:\n " )
@@ -162,7 +156,6 @@ def list_bucket_directory_mode(self, bucket):
162
156
self .response .write (" {}" .format (subdir_file ))
163
157
self .response .write ("\n " )
164
158
165
- # [START delete_files]
166
159
def delete_files (self ):
167
160
self .response .write ("Deleting files...\n " )
168
161
for filename in self .tmp_filenames_to_clean_up :
@@ -173,8 +166,5 @@ def delete_files(self):
173
166
pass
174
167
175
168
176
- # [END delete_files]
177
-
178
-
179
169
app = webapp2 .WSGIApplication ([("/" , MainPage )], debug = True )
180
- # [END sample ]
170
+ # [END gae_storage_sample ]
0 commit comments