Skip to content

Commit

Permalink
Speed up exists function on buckets with large number of files (>10⁵)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mibou committed Apr 7, 2013
1 parent 7b33f7b commit 15751ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/backends/amazon-S3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ To allow ``django-admin.py`` collectstatic to automatically put your static file

STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

``AWS_S3_NUMEROUS_FILES`` (optional)

If you have numerous files in your bucket, some simple functions as `exists` might end up being very slow as it would first check across all the entries. This function can even take several minutes if you have hundred thousand files. This parameter set to `True` speeds up this function.

Fields
------

Expand Down
6 changes: 4 additions & 2 deletions storages/backends/s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class S3BotoStorage(Storage):
custom_domain = setting('AWS_S3_CUSTOM_DOMAIN')
calling_format = setting('AWS_S3_CALLING_FORMAT', SubdomainCallingFormat())
secure_urls = setting('AWS_S3_SECURE_URLS', True)
numerous_files_bucket = setting('AWS_S3_NUMEROUS_FILES', False)
file_name_charset = setting('AWS_S3_FILE_NAME_CHARSET', 'utf-8')
gzip = setting('AWS_IS_GZIPPED', False)
preload_metadata = setting('AWS_PRELOAD_METADATA', False)
Expand Down Expand Up @@ -408,8 +409,9 @@ def delete(self, name):

def exists(self, name):
name = self._normalize_name(self._clean_name(name))
if self.entries:
return name in self.entries
if not self.numerous_files_bucket:
if self.entries:
return name in self.entries
k = self.bucket.new_key(self._encode_name(name))
return k.exists()

Expand Down

0 comments on commit 15751ea

Please sign in to comment.