def read_file(self, filename): self.response.write('Truncated file content:\n') gcs_file = gcs.open(filename) self.response.write(gcs_file.readline()) gcs_file.seek(-1024, os.SEEK_END) self.response.write(gcs_file.read()) gcs_file.close()
def gcs_read_blob(dyn): """ read binary blob from google cloud storage """ gcs_file_name = '/%s/%s' % (default_bucket, dyn.filename) try: with gcs.open(gcs_file_name) as f: return f.read() except gcs.NotFoundError, e: logging.warning('GCS file %s NOT FOUND : %s' % (gcs_file_name, e)) return None
def gcs_zip_dynamics(): """ bonus: save Dynamics and GCS blobs in a zip archive """ gcs_file_name = '/%s/dynamics.zip' % default_bucket with gcs.open(gcs_file_name, 'w', content_type=b'multipart/x-zip') as f: with zipfile.ZipFile(f, 'w') as z: for each in Dynamics.query(): member_dir = each.filename.replace('.', '_').encode('utf-8') z.writestr(b'%s/safe_key.txt' % member_dir, each.key.urlsafe().encode('utf-8')) z.writestr(b'%s/serving_url.txt' % member_dir, each.serving_url.encode('utf-8')) # if we have a GCS blob for this entity, save it in this member blob = gcs_read_blob(each) if blob: z.writestr(b'%s/%s' % (member_dir, each.filename), blob) z.writestr(b'%s/content_type.txt' % member_dir, gcs_content_type(each))
def gcs_write_blob(dyn, blob): """ update google cloud storage dyn entity """ gcs_file_name = '/%s/%s' % (default_bucket, dyn.filename) content_type = mimetypes.guess_type(dyn.filename)[0] if dyn.extension in ['js', 'css']: content_type += b'; charset=utf-8' with gcs.open(gcs_file_name, 'w', content_type=content_type, options={b'x-goog-acl': b'private'}) as f: # f.write(images.resize(blob, width=100, height=100)) try: f.write(images.resize(blob, width=100, height=100)) except: f.write(blob) return gcs_file_name
def create_file(self, filename): """Create a file. The retry_params specified in the open call will override the default retry params for this particular file handle. Args: filename: filename. """ self.response.write('Creating file %s\n' % filename) write_retry_params = gcs.RetryParams(backoff_factor=1.1) gcs_file = gcs.open(filename, 'w', content_type='text/plain', options={'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar'}, retry_params=write_retry_params) gcs_file.write('abcde\n') gcs_file.write('f' * 1024 + '\n') gcs_file.close() self.tmp_filenames_to_clean_up.append(filename)
def create_file2(self, file_name): gcs_file = gcs.open()