command_2.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. def archive(self):
  2. access_key = config.get('ckanext.s3archive.access_key')
  3. secret_key = config.get('ckanext.s3archive.secret_key')
  4. bucket_name = config.get('ckanext.s3archive.bucket')
  5. if not access_key:
  6. print ('ckanext.s3archive.access_key config argument not set')
  7. return
  8. if not secret_key:
  9. print ('ckanext.s3archive.secret_key config argument not set')
  10. return
  11. if not bucket_name:
  12. print ('ckanext.s3archive.bucket config argument not set')
  13. return
  14. storage_path = get_storage_path()
  15. if not storage_path:
  16. print ('ckan.storage_path not set in config')
  17. return
  18. resource_path = os.path.join(storage_path, 'resources')
  19. def walk(bucket, dir, files):
  20. for file in files:
  21. full_path = os.path.join(resource_path, dir, file)
  22. if not os.path.isfile(full_path) or full_path.endswith('~'):
  23. continue
  24. key_name = full_path[len(resource_path):]
  25. for key in bucket.list(prefix=key_name.lstrip('/')):
  26. key.delete()
  27. resource_id = key_name.replace('/', '')
  28. resource = model.Resource.get(resource_id)
  29. if not resource:
  30. continue
  31. last_part = resource.url.split('/')[-1]
  32. file_name = munge.munge_filename(last_part)
  33. key_name = key_name + '/' + file_name
  34. key = s3key.Key(bucket)
  35. key.key = key_name
  36. key.set_contents_from_filename(full_path)
  37. print ('Archived %s' % key_name)
  38. os.remove(full_path)
  39. conn = s3connection.S3Connection(access_key, secret_key)
  40. bucket = conn.get_bucket(bucket_name)
  41. try:
  42. os.path.walk(resource_path, walk, bucket)
  43. finally:
  44. conn.close()