You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by cl...@apache.org on 2021/02/15 15:19:23 UTC

[libcloud] branch trunk updated: Respect common HTTP headers when uploading Azure Storage blob via stream (#1553)

This is an automated email from the ASF dual-hosted git repository.

clewolff pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 55fb3bc  Respect common HTTP headers when uploading Azure Storage blob via stream (#1553)
55fb3bc is described below

commit 55fb3bc94400ea8e006a08fe8a6e916392e07acb
Author: Clemens Wolff <cl...@apache.org>
AuthorDate: Mon Feb 15 10:19:12 2021 -0500

    Respect common HTTP headers when uploading Azure Storage blob via stream (#1553)
    
    * Respect headers uploading Azure blob via stream
    
    * Add tests for _fix_headers
---
 CHANGES.rst                               |  6 ++++++
 libcloud/storage/drivers/azure_blobs.py   | 32 +++++++++++++++++++++++++++++--
 libcloud/test/storage/test_azure_blobs.py | 17 ++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index ea7cf6b..702327d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -7,6 +7,12 @@ Changes in Apache Libcloud 3.3.2
 Storage
 ~~~~~~~
 
+- [Azure Blobs] Respect Content-Encoding, Content-Language and Cache-Control
+  headers when uploading blobs via stream.
+
+  Reported by Veith Röthlingshöfer - @RunOrVeith.
+  (GITHUB-1550)
+
 - [Azure Blobs] Enable the Azure storage driver to be used with
   Azure Government, Azure China, and Azure Private Link by setting
   the driver host argument to the endpoint suffix for the environment.
diff --git a/libcloud/storage/drivers/azure_blobs.py b/libcloud/storage/drivers/azure_blobs.py
index 6c65c51..8cebc73 100644
--- a/libcloud/storage/drivers/azure_blobs.py
+++ b/libcloud/storage/drivers/azure_blobs.py
@@ -826,6 +826,7 @@ class AzureBlobsStorageDriver(StorageDriver):
         response = self._commit_blocks(object_path=object_path,
                                        chunks=chunks,
                                        lease=lease,
+                                       headers=headers,
                                        meta_data=meta_data,
                                        content_type=content_type,
                                        data_hash=data_hash,
@@ -845,7 +846,7 @@ class AzureBlobsStorageDriver(StorageDriver):
             'bytes_transferred': bytes_transferred,
         }
 
-    def _commit_blocks(self, object_path, chunks, lease,
+    def _commit_blocks(self, object_path, chunks, lease, headers,
                        meta_data, content_type, data_hash,
                        object_name, file_path):
         """
@@ -860,7 +861,7 @@ class AzureBlobsStorageDriver(StorageDriver):
 
         data = tostring(root)
         params = {'comp': 'blocklist'}
-        headers = {}
+        headers = headers or {}
 
         lease.update_headers(headers)
         lease.renew()
@@ -880,6 +881,8 @@ class AzureBlobsStorageDriver(StorageDriver):
 
         headers['Content-Length'] = len(data)
 
+        headers = self._fix_headers(headers)
+
         response = self.connection.request(object_path, data=data,
                                            params=params, headers=headers,
                                            method='PUT')
@@ -952,6 +955,31 @@ class AzureBlobsStorageDriver(StorageDriver):
 
         return False
 
+    def _fix_headers(self, headers):
+        """
+        Update common HTTP headers to their equivalent in Azure Storage
+
+        :param headers: The headers dictionary to be updated
+        :type headers: ``dict``
+        """
+        to_fix = (
+            'cache-control',
+            'content-encoding',
+            'content-language',
+        )
+
+        fixed = {}
+
+        for key, value in headers.items():
+            key_lower = key.lower()
+
+            if key_lower in to_fix:
+                fixed['x-ms-blob-%s' % key_lower] = value
+            else:
+                fixed[key] = value
+
+        return fixed
+
     def _update_metadata(self, headers, meta_data):
         """
         Update the given metadata in the headers
diff --git a/libcloud/test/storage/test_azure_blobs.py b/libcloud/test/storage/test_azure_blobs.py
index 19dfcee..23d38ec 100644
--- a/libcloud/test/storage/test_azure_blobs.py
+++ b/libcloud/test/storage/test_azure_blobs.py
@@ -935,6 +935,23 @@ class AzureBlobsTests(unittest.TestCase):
         self.assertEqual(host2, 'fakeaccount2.blob.core.windows.net')
         self.assertEqual(host3, 'test.foo.bar.com')
 
+    def test_normalize_http_headers(self):
+        driver = self.driver_type('fakeaccount1', 'deadbeafcafebabe==')
+
+        headers = driver._fix_headers({
+            # should be normalized to include x-ms-blob prefix
+            'Content-Encoding': 'gzip',
+            'content-language': 'en-us',
+            # should be passed through
+            'x-foo': 'bar',
+        })
+
+        self.assertEqual(headers, {
+            'x-ms-blob-content-encoding': 'gzip',
+            'x-ms-blob-content-language': 'en-us',
+            'x-foo': 'bar',
+        })
+
     def test_storage_driver_host_govcloud(self):
         driver1 = self.driver_type(
             'fakeaccount1', 'deadbeafcafebabe==',