Django-用户在视图中上传了S3文件 [英] Django - User uploaded S3 files in the view

查看:163
本文介绍了Django-用户在视图中上传了S3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,用户可以在其中上传PDF/图像文件到他们的个人资料.这些文件的模型相对简单明了:

class ResumeItemFile(models.Model):
    item = models.ForeignKey(ResumeItem, related_name='attachment_files')
    file = models.FileField(
    max_length=255, upload_to=RandomizedFilePath('resume_attachments'),
    verbose_name=_('Attachment'))
    name = models.CharField(max_length=255, verbose_name=_('Naam'), blank=True)

我正在创建一个视图,其中链接到配置文件(item)的所有文件都收集在一个.zip文件中.我已经在本地工作了,但是在生产环境中,我遇到了以下错误NotImplementedError: This backend doesn't support absolute paths.

主要区别在于,在生产中,媒体文件是通过S3提供的

MEDIA_URL = 'https://******.s3.amazonaws.com/'
STATIC_URL = MEDIA_URL

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

在我看来,我在attachments变量中创建了ResumeItemFile列表,该列表是类似以下内容的字典列表:{'filename', ResumeItemFileObject}

            for file in attachments:

                storage = DefaultStorage()
                filename = file[1]
                file_extension = str(file[0].file).split('.')[-1]
                file_object = storage.open(file[0].file.path, mode='rb')                   
                filename, file_object.read())
                file_object.close()

尽管这在本地可以正常工作,但在暂存时它会在file_object = storage.open(file[0].file.path, mode='rb')行上崩溃.

如果后端不支持绝对路径,我该如何选择正确的文件?有人知道我在做什么错吗?

我认为出现问题是因为在s3boto存储类中,未实现path()方法.根据Django文档

对于无法从本地文件系统访问的存储系统, 这将引发NotImplementedError.

在代码中使用file.name代替file.path.

# file_object = storage.open(file[0].file.path, mode='rb')    
file_object = storage.open(file[0].file.name, mode='rb')

I have a page where users can upload PDF / image files to their profile. The model for these files is relativly straightforward:

class ResumeItemFile(models.Model):
    item = models.ForeignKey(ResumeItem, related_name='attachment_files')
    file = models.FileField(
    max_length=255, upload_to=RandomizedFilePath('resume_attachments'),
    verbose_name=_('Attachment'))
    name = models.CharField(max_length=255, verbose_name=_('Naam'), blank=True)

I am creating a view where all files linked to a profile (item) are gathered in a .zip file. I've got this working locally, but in production I run in the following error NotImplementedError: This backend doesn't support absolute paths.

The main difference is that on production the mediafiles are served through S3

MEDIA_URL = 'https://******.s3.amazonaws.com/'
STATIC_URL = MEDIA_URL

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

In my view I created a list of the ResumeItemFile in the attachments variable, which is a list of dicts that look like this: {'filename', ResumeItemFileObject}

            for file in attachments:

                storage = DefaultStorage()
                filename = file[1]
                file_extension = str(file[0].file).split('.')[-1]
                file_object = storage.open(file[0].file.path, mode='rb')                   
                filename, file_object.read())
                file_object.close()

Though this works fine locally, on staging it crashes on the file_object = storage.open(file[0].file.path, mode='rb') line.

If the backend does not support absolute paths, how I am to select the correct file? Does anyone have an idea of what I am doing wrong?

解决方案

I think that problem comes because in the s3boto storage class, the path() method is not implemented. As per the Django documentation,

For storage systems that aren’t accessible from the local filesystem, this will raise NotImplementedError instead.

Instead of file.path use file.name in your code.

# file_object = storage.open(file[0].file.path, mode='rb')    
file_object = storage.open(file[0].file.name, mode='rb')

这篇关于Django-用户在视图中上传了S3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆