将S3上传/浏览与django-tinymce集成

时间:2011-10-13 18:37:02

标签: django amazon-s3 tinymce

我一直在寻找有关如何将Amazon S3与TinyMCE集成的资源。我到目前为止提出的最佳链接是:http://forums.aurigma.com/yaf_postst4033_Amazon-S3-File-Manager-for-TinyMCE-and-CKEditor.aspx

有没有人有经验将它集成到Django应用程序中?如果没有,有哪些替代解决方案可用于富文本编辑和从S3中提取图像?

2 个答案:

答案 0 :(得分:2)

我最终使用django-storages。效果很好。

答案 1 :(得分:1)

如果有人像我一样最近进行搜索,并且需要一种解决方案来使django-tinymce4-lite与django-storages和django-filebrowser-no-grappelli配合使用,我可以通过执行以下操作使其工作:

1)遵循这个出色的教程:https://fosstack.com/how-to-set-up-tinymce-in-django-app/

2)当要使s3部分正常工作时,您需要按照说明here

中的说明安装django-storages并设置媒体存储。

3)您需要创建S3Boto3Storage的子类,并使您的DEFAULT_FILE_STORAGE如下:

DEFAULT_FILE_STORAGE = 'module.containing.MediaStorage'

4)并在该模块内使用以下API创建MediaStorage类以与FileBrowser一起使用

class MediaStorage(S3Boto3Storage):
  location = settings.MEDIAFILES_LOCATION

  def isdir(self, name):
      if not name:  # Empty name is a directory
          return True

      if self.isfile(name):
          return False

      for item in super(MediaStorage, self).listdir(name):
          if len(item):
              return True

      return False

  def isfile(self, name):
      try:
          name = self._normalize_name(self._clean_name(name))
          f = S3Boto3StorageFile(name, 'rb', self)
          if "directory" in f.obj.content_type:
              return False
          return True
      except Exception:
          return False

  def makedirs(self, name):
      name = self._normalize_name(self._clean_name(name))
      return self.bucket.meta.client.put_object(Bucket=self.bucket.name, Key=f'{name}/')

  def rmtree(self, name):
      name = self._normalize_name(self._clean_name(name))
      delete_objects = [{'Key': f"{name}/"}]

      dirlist = self.listdir(self._encode_name(name))
      for item in dirlist:
          for obj in item:
              obj_name = f"{name}/{obj}"
              if self.isdir(obj_name):
                  obj_name = f"{obj_name}/"
              delete_objects.append({'Key': obj_name})
      self.bucket.delete_objects(Delete={'Objects': delete_objects})

  def path(self, name):
      return name

  def listdir(self, name):
      directories, files = super().listdir(name)
      if '.' in files:
          files.remove('.')
      return directories, files

  def exists(self, name):
      if self.isdir(name):
          return True
      else:
          return super().exists(name)

  def get_modified_time(self, name):
      # S3 boto3 library requires that directorys have the trailing slash
      if self.isdir(name):
          name = f'{name}/'
      return super().get_modified_time(name)

  def size(self, name):
      # S3 boto3 library requires that directorys have the trailing slash
      if self.isdir(name):
          name = f'{name}/'
      return super().size(name)

希望这会有所帮助