README.rst 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. flask-admin-s3-upload
  2. =====================
  3. Field types for allowing file and image uploads to Amazon S3 (as well as default local storage) in Flask-Admin.
  4. Example
  5. -------
  6. For a complete, working Flask app that demonstrates flask-admin-s3-upload in action, have a look at `flask-s3-save-example <https://github.com/Jaza/flask-s3-save-example>`_.
  7. Usage
  8. -----
  9. Use with a Flask-Admin ModelView by overriding field types, and by passing in special arguments to those fields:
  10. .. code-block:: python
  11. from flask.ext.admin.contrib.sqla import ModelView
  12. class MyView(ModelView):
  13. form_overrides = dict(
  14. some_image=S3ImageUploadField,
  15. some_file=S3FileUploadField)
  16. form_args = dict(
  17. some_image=dict(
  18. base_path='/some/folder/static',
  19. relative_path='some_image/',
  20. url_relative_path='uploads/',
  21. namegen=your_namegen_func_here,
  22. storage_type_field='some_image_storage_type',
  23. bucket_name_field='some_image_storage_bucket_name',
  24. ),
  25. some_file=dict(
  26. base_path='/some/folder/static',
  27. relative_path='some_file/',
  28. namegen=your_namegen_func_here,
  29. allowed_extensions=('pdf', 'txt'),
  30. storage_type_field='some_file_storage_type',
  31. bucket_name_field='some_file_storage_bucket_name',
  32. ))
  33. def scaffold_form(self):
  34. # Note: assuming that we have Flask-S3 config values to pass
  35. # to fields below. Flask-S3 is not required, you can pass
  36. # values from elsewhere if you want.
  37. from flask import current_app as app
  38. form_class = super(MyView, self).scaffold_form()
  39. static_root_parent = '/some/folder'
  40. if app.config['USE_S3']:
  41. form_class.some_image.kwargs['storage_type'] = 's3'
  42. form_class.some_file.kwargs['storage_type'] = 's3'
  43. form_class.some_image.kwargs['bucket_name'] = app.config['S3_BUCKET_NAME']
  44. form_class.some_image.kwargs['access_key_id'] = app.config['AWS_ACCESS_KEY_ID']
  45. form_class.some_image.kwargs['access_key_secret'] = app.config['AWS_SECRET_ACCESS_KEY']
  46. form_class.some_image.kwargs['static_root_parent'] = static_root_parent
  47. form_class.some_file.kwargs['bucket_name'] = app.config['S3_BUCKET_NAME']
  48. form_class.some_file.kwargs['access_key_id'] = app.config['AWS_ACCESS_KEY_ID']
  49. form_class.some_file.kwargs['access_key_secret'] = app.config['AWS_SECRET_ACCESS_KEY']
  50. form_class.some_file.kwargs['static_root_parent'] = static_root_parent
  51. return form_class