소스 검색

New Feature: Upload _file_ to S3
1. New function `upload_from_file` is added. Accordingly, interfaces and `AWSBucketObject` are updated. The signature of the function is added to `OpenStackBucketObject`, but not implemented.
2. Test codes for AWS `upload_from_file` added.

vahid 9 년 전
부모
커밋
e5b4bff142

+ 20 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -2037,6 +2037,26 @@ class BucketObject(CloudResource):
         """
         pass
 
+    @abstractmethod
+    def upload_from_file(self, path):
+        """
+        Stores the contents of the file pointed by the "path" variable.
+        :param path: Absolute path to the file to be uploaded to S3.
+        :return: void
+        """
+        pass
+
+    @abstractmethod
+    def upload_from_large_file(self, path):
+        """
+        Stores the contents of the large file pointed by the "path" variable.
+        This function split the file in smaller chunks, and uploads chunks
+        in turn.
+        :param path: Absolute path to the large file to be uploaded to S3.
+        :return: void
+        """
+        pass
+
     @abstractmethod
     def delete(self):
         """

+ 19 - 0
cloudbridge/cloud/providers/aws/resources.py

@@ -807,6 +807,25 @@ class AWSBucketObject(BaseBucketObject):
         """
         self._key.set_contents_from_string(data)
 
+
+    def upload_from_file(self, path):
+        """
+        Stores the contents of the file pointed by the "path" variable.
+        :param path: Absolute path to the file to be uploaded to S3.
+        :return: void
+        """
+        self._key.set_contents_from_filename(path)
+
+    def upload_from_large_file(self, path):
+        """
+        Stores the contents of the large file pointed by the "path" variable.
+        This function split the file in smaller chunks, and uploads chunks
+        in turn.
+        :param path: Absolute path to the large file to be uploaded to S3.
+        :return: void
+        """
+        raise NotImplementedError("This functionality is not implemented yet.")
+
     def delete(self):
         """
         Delete this object.

+ 20 - 0
cloudbridge/cloud/providers/openstack/resources.py

@@ -1053,6 +1053,26 @@ class OpenStackBucketObject(BaseBucketObject):
         self._provider.swift.put_object(self.cbcontainer.name, self.name,
                                         data)
 
+    def upload_from_file(self, path):
+        """
+        Stores the contents of the file pointed by the "path" variable.
+        :param path: Absolute path to the file to be uploaded to S3.
+        :return: void
+        """
+        raise NotImplementedError("This functionality is not implemented yet.")
+
+
+    def upload_from_large_file(self, path):
+        """
+        Stores the contents of the large file pointed by the "path" variable.
+        This function split the file in smaller chunks, and uploads chunks
+        in turn.
+        :param path: Absolute path to the large file to be uploaded to S3.
+        :return: void
+        """
+        raise NotImplementedError("This functionality is not implemented yet.")
+
+
     def delete(self):
         """
         Delete this object.

+ 17 - 1
test/test_object_store_service.py

@@ -137,6 +137,7 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                 "Object %s should have been deleted but still exists." %
                 obj_name)
 
+
     def test_upload_download_bucket_content(self):
 
         name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
@@ -145,9 +146,9 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
         with helpers.cleanup_action(lambda: test_bucket.delete()):
             obj_name = "hello_upload_download.txt"
             obj = test_bucket.create_object(obj_name)
+            content = b"Hello World. Here's some content."
 
             with helpers.cleanup_action(lambda: obj.delete()):
-                content = b"Hello World. Here's some content."
                 # TODO: Upload and download methods accept different parameter
                 # types. Need to make this consistent - possibly provider
                 # multiple methods like upload_from_file, from_stream etc.
@@ -159,3 +160,18 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                 for data in obj.iter_content():
                     target_stream2.write(data)
                 self.assertEqual(target_stream2.getvalue(), content)
+
+
+            obj_name = "hello_upload_download.txt"
+            obj = test_bucket.create_object(obj_name)
+            with helpers.cleanup_action(lambda: obj.delete):
+                with open("hello_upload_download.txt", "w+") as tmpFile:
+                    tmpFile.write(content)
+                    obj.upload_from_file(tmpFile.name)
+                    target_stream = BytesIO()
+                    obj.save_content(target_stream)
+                    self.assertEqual(target_stream.getvalue(), content)
+                    target_stream2 = BytesIO()
+                    for data in obj.iter_content():
+                        target_stream2.write(data)
+                    self.assertEqual(target_stream2.getvalue(), content)