14-shutil.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import boto3
  2. import os as base_os
  3. class Shutil():
  4. def __init__(self, os):
  5. self.os = os
  6. def authorize(self, bucketname, aws_access_key_id=None, aws_secret_access_key=None):
  7. self.s3_r = boto3.resource('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
  8. self.s3_c = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
  9. self.bucketname = bucketname
  10. self.bucket = self.s3_r.Bucket(bucketname)
  11. def copy(self, src, dst, upload=False, download=False):
  12. '''
  13. Copy files from src to dst.
  14. Restrictions
  15. ============
  16. 1. src - file only
  17. 2. dst - file or directory
  18. Below options are availaible
  19. ============================
  20. when both src and dst are s3 - upload=False, download=False [Default]
  21. when src is local and dst is s3 - upload=True, download=False
  22. when src is s3 and dsr is local - upload=False, download=True
  23. '''
  24. if upload == False and src.startswith("/"):
  25. src = src[1:]
  26. if download == False and dst.startswith("/"):
  27. dst = dst[1:]
  28. if all([upload, download]):
  29. raise Exception("Both upload and download cannot be set as true")
  30. elif not any([upload, download]):
  31. if not self.os.path.exists(src):
  32. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  33. if self.os.path.isdir("/" + src):
  34. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  35. # We can also throw exception, if parent directory not present, lets just ignore now
  36. # if not self.os.path.exists(self.os.path.dirname("/" + dst)):
  37. # raise Exception("Parent directory not present %s" % (self.os.path.dirname("/" + dst)))
  38. if self.os.path.isdir("/" + dst):
  39. dst = dst.rstrip("/") + "/" + self.os.path.basename(src)
  40. self.s3_r.Object(self.bucketname, dst).copy_from(CopySource=self.bucketname+"/"+src)
  41. elif download:
  42. if not self.os.path.exists(src):
  43. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  44. if self.os.path.isdir("/" + src):
  45. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  46. if base_os.path.isdir(dst):
  47. dst = dst.rstrip("/") + "/" + basename(src)
  48. self.s3_c.download_file(self.bucketname, src, dst)
  49. elif upload:
  50. if not base_os.path.exists(src):
  51. raise Exception("src (%s) does not exists in local" % (src))
  52. if base_os.path.isdir(src):
  53. raise Exception("src (%s) cannot be a directory" % (src))
  54. # if not self.os.path.exists(self.os.path.dirname("/" + dst)):
  55. # raise Exception("Parent directory not present %s" % (self.os.path.dirname("/" + dst)))
  56. if self.os.path.isdir("/" + dst):
  57. dst = dst.rstrip("/") + "/" + self.os.path.basename(src)
  58. self.s3_c.upload_file(src, self.bucketname, dst)
  59. def copyfile(self, src, dst, upload=False, download=False):
  60. '''
  61. Copy files from src to dst.
  62. Restrictions
  63. ============
  64. 1. src - file only
  65. 2. dst - file only
  66. Below options are availaible
  67. ============================
  68. when both src and dst are s3 - upload=False, download=False [Default]
  69. when src is local and dst is s3 - upload=True, download=False
  70. when src is s3 and dsr is local - upload=False, download=True
  71. '''
  72. dst = dst.rstrip("/")
  73. if upload == False and src.startswith("/"):
  74. src = src[1:]
  75. if download == False and dst.startswith("/"):
  76. dst = dst[1:]
  77. try:
  78. username = 1
  79. password = 2
  80. except Exception:
  81. print(1)
  82. if all([upload, download]):
  83. raise Exception("Both upload and download cannot be set as true")
  84. elif not any([upload, download]):
  85. '''Condition check - Src directory exists in s3 and both src and dst should not be directory'''
  86. if not self.os.path.exists(src):
  87. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  88. if self.os.path.isdir("/" + src):
  89. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  90. if self.os.path.isdir("/" + dst):
  91. raise Exception("dst (%s) cannot be a directory" % ("/" + dst))
  92. self.s3_r.Object(self.bucketname, dst).copy_from(CopySource=self.bucketname + "/" + src)
  93. elif download:
  94. if not self.os.path.exists(src):
  95. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  96. if self.os.path.isdir("/" + src):
  97. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  98. if base_os.path.isdir(dst):
  99. raise Exception("dst (%s) cannot be a directory" % (dst))
  100. self.s3_c.download_file(self.bucketname, src, dst)
  101. elif upload:
  102. if not base_os.path.exists(src):
  103. raise Exception("src (%s) does not exists in local" % (src))
  104. if base_os.path.isdir(src):
  105. raise Exception("src (%s) cannot be a directory" % (src))
  106. if self.os.path.isdir("/" + dst):
  107. raise Exception("dst (%s) cannot be a directory" % ("/" + dst))
  108. self.s3_c.upload_file(src, self.bucketname, dst)
  109. def copytree(self):
  110. raise Exception("Not implemented in this version")
  111. def rmtree(self, x):
  112. '''
  113. Remove directory tree s3.
  114. Restrictions
  115. ============
  116. x - dir only
  117. '''
  118. x = x.lstrip("/")
  119. if not self.os.path.exists(x):
  120. raise Exception("(%s) does not exists in s3" % (x))
  121. if not self.os.path.isdir(x):
  122. raise Exception("%s is not a directory" % (x))
  123. for bucket_object in self.bucket.objects.filter(Prefix=x):
  124. # Filtering the keys for immediate directories
  125. self.s3_r.Object(self.bucketname, bucket_object.key).delete()
  126. def move(self, src, dst, upload=False, download=False):
  127. '''
  128. move files from src to dst.
  129. Restrictions
  130. ============
  131. 1. src - file only
  132. 2. dst - file or directory
  133. Below options are availaible
  134. ============================
  135. when both src and dst are s3 - upload=False, download=False [Default]
  136. when src is local and dst is s3 - upload=True, download=False
  137. when src is s3 and dsr is local - upload=False, download=True
  138. '''
  139. if upload is False and src.startswith("/"):
  140. src = src[1:]
  141. if download == False and dst.startswith("/"):
  142. dst = dst[1:]
  143. if all([upload, download]):
  144. raise Exception("Both upload and download cannot be set as true")
  145. elif not any([upload, download]):
  146. if not self.os.path.exists(src):
  147. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  148. if self.os.path.isdir("/" + src):
  149. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  150. if self.os.path.isdir("/" + dst):
  151. dst = dst.rstrip("/") + "/" + self.os.path.basename(src)
  152. self.s3_r.Object(self.bucketname, dst).copy_from(CopySource=self.bucketname+"/"+src)
  153. self.s3_r.Object(self.bucketname, src).delete()
  154. elif download:
  155. if not self.os.path.exists(src):
  156. raise Exception("src (%s) does not exists in s3" % ("/" + src))
  157. if self.os.path.isdir("/" + src):
  158. raise Exception("src (%s) cannot be a directory" % ("/" + src))
  159. if base_os.path.isdir(dst):
  160. dst = dst.rstrip("/") + "/" + self.os.path.basename(src)
  161. self.s3_c.download_file(self.bucketname, src, dst)
  162. self.s3_r.Object(self.bucketname, src).delete()
  163. elif upload:
  164. if not base_os.path.exists(src):
  165. raise Exception("src (%s) does not exists in local" % (src))
  166. if base_os.path.isdir(src):
  167. raise Exception("src (%s) cannot be a directory" % (src))
  168. if self.os.path.isdir("/" + dst):
  169. dst = dst.rstrip("/") + "/" + self.os.path.basename(src)
  170. self.s3_c.upload_file(src, self.bucketname, dst)
  171. base_os.remove(src)
  172. def make_archive(self):
  173. raise Exception("Not implemented in this version")