s3sync_pending_3.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. def upload_pending_to_s3(self):
  2. """Gets the pending filenames from cache and uploads them."""
  3. pending_key = get_pending_key()
  4. pending = cache.get(pending_key, [])
  5. remaining = []
  6. for i, file_key in enumerate(pending):
  7. prefixed_file_key = '%s/%s' % (self.prefix, file_key)
  8. if self.verbosity > 0:
  9. print ("Uploading %s..." % prefixed_file_key)
  10. if self.dry_run:
  11. self.upload_count += 1
  12. continue
  13. filename = self.DIRECTORY + '/' + file_key
  14. failed = True
  15. try:
  16. upload_file_to_s3(prefixed_file_key, filename, self.key,
  17. do_gzip=True, do_expires=True)
  18. except boto.exception.S3CreateError as e:
  19. # TODO: retry to create a few times
  20. print ("Failed to upload: %s" % e)
  21. except Exception as e:
  22. print (e)
  23. raise
  24. else:
  25. failed = False
  26. self.upload_count += 1
  27. cache.delete(file_key)
  28. finally:
  29. if failed:
  30. remaining.append(file_key)
  31. self.remaining_count += 1
  32. if not self.dry_run:
  33. cache.set(pending_key, remaining)