django和amazon s3的轻量级文件上传程序输入

django-s3file的Python项目详细描述


django和amazon s3的轻量级文件上传输入。

django-s3file允许您有效地直接上传文件 绕过应用服务器。这样可以避免长时间跑步 大文件上传请求。这对于 您在aws lambda或heroku上运行服务,在那里您有一个硬请求 限制。

PyPi VersionBuild StatusTest CoverageGitHub license

功能

  • 轻量级:少于200行
  • 没有javascript或python依赖项(没有jquery)
  • 易于集成
  • 就像内置的
  • 可扩展javascript api

对于书呆子

http-message-flow.svg

安装

请确保Amazon S3 storage设置正确。

只需使用pip安装s3file。

pip install django-s3file
# or
pipenv install django-s3file

在您的设置中添加s3file应用程序和中间件:

INSTALLED_APPS=('...','s3file','...',)MIDDLEWARE=('...','s3file.middleware.S3FileMiddleware','...',)

用法

s3file自动替换django的ClearableFileInputwidget, 你根本不需要修改你的代码。

ClearableFileInput小部件只会被自动替换 当DEFAULT_FILE_STORAGE设置设置为 django-storages'S3Boto3Storage

设置aws s3 bucket

上载文件夹

s3file上传到单个文件夹。文件稍后由django在 它们被保存到upload_to位置。

建议该文件夹setup expiration,以确保 旧的和未使用的文件上传不会增加和产生成本。

默认文件夹名是:tmp/s3file您可以通过更改 S3FILE_UPLOAD_PATH设置。

CORS政策

您需要允许来自所有来源的POST。只需添加以下内容 你的公司政策。

<CORSConfiguration><CORSRule><AllowedOrigin>*</AllowedOrigin><AllowedMethod>POST</AllowedMethod><MaxAgeSeconds>3000</MaxAgeSeconds><AllowedHeader>*</AllowedHeader></CORSRule></CORSConfiguration>

进度条

s3file确实发出了可以用来显示某种进度条的进度信号。 为每个单独的文件输入以及 作为一个整体。

进程信号包含以下详细信息:

console.log(event.detail){progress:0.4725307607171312// total upload progress of either a form or single input
loaded:1048576// total upload progress of either a form or single input
total:2219064// total bytes to upload
currentFile:File{}// file object
currentFileName:"text.txt"// file name of the file currently uploaded
currentFileProgress:0.47227834703299176// upload progress of that file
originalEvent:ProgressEvent{}// the original XHR onprogress event
}

下面的示例实现了一个boostrap进度条,用于上载 整个表格。

<divclass="progress"><divclass="progress-bar"role="progressbar"style="width: 0%;"aria-valuenow="0"aria-valuemin="0"aria-valuemax="100">0%</div></div>
(function(){varform=document.getElementsByTagName('form')[0]varprogressBar=document.getElementsByClassName('progress-bar')[0]form.addEventListener('progress',function(event){// event.detail.progress is a value between 0 and 1
varpercent=Math.round(event.detail.progress*100)progressBar.setAttribute('style','width:'+percent+'%')progressBar.setAttribute('aria-valuenow',percent)progressBar.innerText=percent+'%'})})()

在开发中使用s3file

在开发中使用s3file可能会有帮助,特别是如果您想使用进度 上述信号。因此,s3file附带了一个aws s3虚拟后端。 它的行为类似于真正的s3存储后端。如果 DEFAULT_FILE_STORAGE设置设置为FileSystemStorage

防止用户意外地使用FileSystemStorage和不安全的s3 虚拟后端在生产中,还有一个附加的部署检查 运行Django的部署检查套件时出错:

python manage.py check --deploy

我们建议始终将部署检查套件作为部署的一部分运行 管道。

上传多个文件

django对uploading multiple files的支持确实有限。S3文件 完全支持此功能。自定义中间件确保 文件可以通过request.FILES访问,即使它们已经 直接上传到aws s3而不是django应用服务器。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
c#Java Tcp服务器和。Net Tcp客户端的发送和接收问题   安卓应用程序上的java标记地理位置,其位置位于我周围5Km半径范围内。   向java添加对话框并检索html文件   当eclipse甚至无法打开时,java会在eclipse中更改不兼容的JVM   java中同一jframe中的jlabel和paintComponent   基于另一数组排序的java排序   java AADSTS7000012:该补助金是为另一个租户获得的   java在JSF中使用foreach循环   java如何通过maven为运行junit测试创建运行配置?   java Selenium webDriver不稳定错误堆栈跟踪   java有没有办法创建以键为大写的JSON对象?