这一直让我发疯,我无法弄清楚问题出在哪里。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