在临时内存中访问用户上传的视频时出现问题

Dan*_*ill 3 python youtube django

我正在尝试使用 html 输入类型file和 python 模块 youtube-upload 将用户上传的视频提交到 youtube 。提交表单时,它的处理方式如下:

if request.method == 'POST':
    video = request.FILES['file']

    v=str(video)

    command = 'youtube-upload --email=email@gmail.com --password=password --title=title --description=description --category=Sports ' + v

    r = subprocess.Popen(command,   stdout=subprocess.PIPE)
    v = r.stdout.read()
Run Code Online (Sandbox Code Playgroud)

所以我认为问题是我需要为视频提供更完整的路径。如果是这种情况,访问临时内存中视频的路径是什么。

命令的通用 for 是: youtube-upload --email=email --password=password --title=title --description=description --category=category video.avi

或者,我已经在这里专门查看了 youtube api 但是如果有人可以提供有关如何使用 api 在 python 中执行此操作的更完整的解释,那将是惊人的。不幸的是,网站上的指南只关注 xml。

在 sacabuche 的评论之后编辑:

所以我的观点现在大致是:

def upload_video(request):
     if request.method == 'POST':
         video = request.FILE['file']
         v = video.temporary_file_path
         command = 'youtube-upload --email=email@gmail.com --password=password --title=title --description=description --category=Sports ' + v   

         r=subprocess.Popen(command, stdout=subprocess.PIPE)

         vid = r.stdout.read()
     else:
         form = VideoForm()
         request.upload_handlers.pop(0)
     return render_to_response('create_check.html', RequestContext(request, locals() ) )
Run Code Online (Sandbox Code Playgroud)

v=video.temporary_file_path得出错误'InMemoryUploadedFile' object has no attribute 'temporary_file_path'。所以视频仍然在临时内存中,我不知道temporary_file_path应该调用什么对象或如何获取所述对象。

sac*_*che 5

实际上 django 将文件保存在内存中,但大文件保存在路径中。
“大文件”的大小可以使用设置中定义FILE_UPLOAD_MAX_MEMORY_SIZE
,并
FILE_UPLOAD_HANDLERS 默认情况下是:

("django.core.files.uploadhandler.MemoryFileUploadHandler",
 "django.core.files.uploadhandler.TemporaryFileUploadHandler",)
Run Code Online (Sandbox Code Playgroud)

这给了我们两种可能性:

1. 移除内存处理程序

删除..MemoryFileUploadHandler但您的所有文件都将保存在临时文件中,这并不酷

2. 动态修改处理程序

文档在这里

#views.py

def video_upload(request):
    # this removes the first handler (MemoryFile....)
    request.upload_handlers.pop(0)
    return _video_upload(request)

def _video_upload(request):
    ....
Run Code Online (Sandbox Code Playgroud)

要获取文件路径,您只需要做 video.temporary_file_path