Exemple #1
0
  def write_file(self, path, content, policy='public-read'):
    if isinstance(content, unicode):
      content = content.encode('utf-8')
    path = path.lstrip('/')
    mimetype = mimetypes.guess_type(path)[0]
    fp = cStringIO.StringIO()
    fp.write(content)
    size = fp.tell()

    try:
      if self.use_interoperable_auth:
        file_key = key.Key(self.bucket)
        file_key.key = path
        headers = {'Cache-Control': 'no-cache'}  # TODO(jeremydw): Better headers.
        if mimetype:
          headers['Content-Type'] = mimetype
        file_key.set_contents_from_file(fp, headers=headers, replace=True, policy=policy,
                                        size=size, rewind=True)
      else:
        file_key = self.bucket.new_key(path)
        file_key.set_contents_from_file(fp, content_type=mimetype, size=size, rewind=True)
        if policy == 'private':
          acl = file_key.get_acl()
          acl.all().revoke_read().revoke_write()
          file_key.save_acl(acl)
    finally:
      fp.close()
Exemple #2
0
 def delete_file(self, path):
   if self.use_interoperable_auth:
     file_key = key.Key(self.bucket)
     file_key.key = path.lstrip('/')
     self.bucket.delete_key(file_key)
   else:
     self.bucket.delete_key(path)
Exemple #3
0
 def read_file(self, path):
     file_key = key.Key(self.bucket)
     file_key.key = path
     try:
         return file_key.get_contents_as_string()
     except boto.exception.GSResponseError as e:
         if e.status != 404:
             raise
         raise IOError('File not found: {}'.format(path))
 def write_file(self, rendered_doc, policy='public-read'):
     path = rendered_doc.path
     content = rendered_doc.read()
     path = path.lstrip('/')
     path = path if path != '' else self.config.main_page_suffix
     fp = cStringIO.StringIO()
     fp.write(content)
     size = fp.tell()
     try:
         file_key = key.Key(self.bucket)
         file_key.key = path
         headers = self._get_headers_for_path(path)
         file_key.set_contents_from_file(
             fp, headers=headers, replace=True, policy=policy, size=size,
             rewind=True)
     finally:
         fp.close()
Exemple #5
0
 def write_file(self, path, content, policy='public-read'):
     if isinstance(content, unicode):
         content = content.encode('utf-8')
     path = path.lstrip('/')
     path = path if path != '' else self.config.main_page_suffix
     fp = cStringIO.StringIO()
     fp.write(content)
     size = fp.tell()
     try:
         file_key = key.Key(self.bucket)
         file_key.key = path
         headers = self._get_headers_for_path(path)
         file_key.set_contents_from_file(
             fp, headers=headers, replace=True, policy=policy, size=size,
             rewind=True)
     finally:
         fp.close()
Exemple #6
0
 def delete_file(self, path):
     file_key = key.Key(self.bucket)
     file_key.key = path.lstrip('/')
     self.bucket.delete_key(file_key)