Python 如何使用S3存储后端编辑Django字段文件

Python 如何使用S3存储后端编辑Django字段文件,python,django,amazon-web-services,amazon-s3,Python,Django,Amazon Web Services,Amazon S3,我已经设置了S3存储后端,可以将文件上传到S3存储桶。我正在使用boto和django存储模块。settings.py中的配置如下所示: AWS_STORAGE_BUCKET_NAME = 'xxxxx' AWS_ACCESS_KEY_ID = 'xxxxxxxxxxxxx' AWS_SECRET_ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxx' AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAG

我已经设置了S3存储后端,可以将文件上传到S3存储桶。我正在使用
boto
django存储
模块。
settings.py
中的配置如下所示:

AWS_STORAGE_BUCKET_NAME = 'xxxxx'
AWS_ACCESS_KEY_ID = 'xxxxxxxxxxxxx'
AWS_SECRET_ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
>>> m.file.path
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../site-packages/django/db/models/fields/files.py", line 64, in _get_path
return self.storage.path(self.name)
  File ".../site-packages/django/core/files/storage.py", line 111, in path
    raise NotImplementedError("This backend doesn't support absolute paths.")
NotImplementedError: This backend doesn't support absolute paths.
以及
models.py
中的类:

class Model (models.Model):
    name            = models.CharField(max_length=50, null=False, blank=False)
    file            = models.FileField(upload_to='models/%Y/%m/%d/')
我需要的是用一个外部程序修改文件,这在默认存储后端(使用本地磁盘)中是可能的,如下所示:

def modify_model(model):
    try:
        subprocess.check_call(["/usr/bin/program", model.file.path])
    except:
        pass
由于S3后端不支持path属性,我想知道应该如何正确地执行它?错误如下所示:

AWS_STORAGE_BUCKET_NAME = 'xxxxx'
AWS_ACCESS_KEY_ID = 'xxxxxxxxxxxxx'
AWS_SECRET_ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
>>> m.file.path
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../site-packages/django/db/models/fields/files.py", line 64, in _get_path
return self.storage.path(self.name)
  File ".../site-packages/django/core/files/storage.py", line 111, in path
    raise NotImplementedError("This backend doesn't support absolute paths.")
NotImplementedError: This backend doesn't support absolute paths.

这可以按预期工作,但是-是否有办法在本机(使用S3后端)使用Django FieldFile类来修改文件而不创建临时文件?

建议使用链接问题中的
boto
库来轻松访问文件。由于
django存储
模块使用
boto
库,因此使用内置django类以本机方式处理文件的可能性有限。(例如
model.file.read()
方法)。我只是想知道FieldFile类对象是否可以像文件系统上的普通文件一样工作。很抱歉,这不是链接answeer的重点。它是linux s3文件系统。这是一个可以接受的答案,但它看起来仍然不是一个轻量级的解决方案。我还以为会有更像蟒蛇的东西呢。