这让我发疯,我无法弄清问题在哪里。 AWS现在遇到问题,但是这个问题在所有这些问题发生之前就已经发生并继续表现出相同的行为。
总结:我有一个管理字段,可以将图像上传到S3并将路径存储在数据库中。重新启动apache,它可以正常运行大约20分钟。然后它停止工作,只是挂起'等待来自域的响应'就是这样,没有错误消息,只是坐在那里,等待,并且在我重新启动apache之前不会再次工作。重新启动apache之后,它可以完美地运行直到它停止,这发生在20分钟之后,我可以告诉并且不是由或多或少的流量或一定数量的插入,删除等引起的。
以下是该模型的代码:
class SampleImage(models.Model):
def __unicode__(self):
return self.name
name = models.CharField(max_length=50)
front_image = S3EnabledImageField(upload_to='samples')
back_image = S3EnabledImageField(upload_to='samples')
S3EnabledImageField代码:
class S3EnabledImageField(models.ImageField):
def generate_filename(self, instance, filename):
path_join = os.path.join(self.get_directory_name(), self.get_filename(filename))
return path_join.replace("\\", "/")
def __init__(self, bucket=settings.BUCKET_NAME, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
if settings.USE_AMAZON_S3:
self.connection = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
if not self.connection.lookup(bucket):
self.connection.create_bucket(bucket)
self.bucket = self.connection.get_bucket(bucket)
kwargs['storage'] = S3Storage(self.bucket)
super(S3EnabledImageField, self).__init__(verbose_name, name, width_field, height_field, **kwargs)
S3Storage的代码:
class S3Storage(FileSystemStorage):
def __init__(self, bucket=None, location=None, base_url=None):
if location is None:
location = settings.MEDIA_ROOT
if base_url is None:
base_url = settings.MEDIA_URL
self.location = os.path.abspath(location)
self.bucket = bucket
self.base_url = base_url
def _open(self, name, mode='rb'):
class S3File(File):
def __init__(self, key):
self.key = key
def size(self):
return self.key.size
def read(self, *args, **kwargs):
return self.key.read(*args, **kwargs)
def write(self, content):
self.key.set_contents_from_string(content)
def close(self):
self.key.close()
return S3File(Key(self.bucket, name))
def _save(self, name, content):
key = Key(self.bucket, name)
if hasattr(content, 'temporary_file_path'):
key.set_contents_from_filename(content.temporary_file_path())
elif isinstance(content, File):
key.set_contents_from_file(content)
else:
key.set_contents_from_string(content)
key.make_public()
return name
def delete(self, name):
self.bucket.delete_key(name)
def exists(self, name):
return Key(self.bucket, name).exists()
def listdir(self, path):
return [key.name for key in self.bucket.list()]
def path(self, name):
raise NotImplementedError
def size(self, name):
return self.bucket.get_key(name).size
def url(self, name):
return Key(self.bucket, name).generate_url(100000)
def get_available_name(self, name):
return name
答案 0 :(得分:0)
通过查看您的代码不确定。但是,听起来可能有连接没有关闭?无论如何,为什么不与之合作:
https://bitbucket.org/david/django-storages/wiki/S3Storage
django-storages对我来说效果很好。