Преглед изворни кода

Minor fixes to signed urls in azure and openstack

Nuwan Goonasekera пре 4 година
родитељ
комит
11a4af4cd4

+ 1 - 5
cloudbridge/providers/azure/azure_client.py

@@ -465,11 +465,7 @@ class AzureClient(object):
             permission=BlobSasPermissions(read=True, write=writable), expiry=expiry,
             user_delegation_key=delegation_key
         )
-        url = (
-            f"https://{self.storage_account}.blob.core.windows.net/"
-            f"{container_name}/{blob_name}?{sas}"
-        )
-
+        url = f"https://{self.storage_account}.blob.core.windows.net/{container_name}/{blob_name}?{sas}"
         return url
 
     def create_empty_disk(self, disk_name, params):

+ 1 - 1
cloudbridge/providers/openstack/resources.py

@@ -1317,7 +1317,7 @@ class OpenStackBucketObject(BaseBucketObject):
         return result
 
     def generate_url(self, expires_in, writable=False):
-        http_method = "POST" if writable else "GET"
+        http_method = "PUT" if writable else "GET"
         # Set a temp url key on the object (http://bit.ly/2NBiXGD)
         temp_url_key = "cloudbridge-tmp-url-key"
         self._provider.swift.post_account(

+ 3 - 4
docs/topics/object_storage.rst

@@ -116,8 +116,7 @@ With your signed URL, you or someone on your team can upload a file like this
     import requests
 
     content = b"Hello world!"
-    # Example for AWS
-    requests.put(url["url"], data=url["fields"], files={"file": ("my-file.txt", content)})
-
-    # Example for GCP/Azure
+    # Only Azure requires the x-ms-blob-type header to be present, but there's no harm
+    # in sending this in for all providers.
+    headers = {'x-ms-blob-type': 'BlockBlob'}
     requests.put(url, data=content)

+ 11 - 11
tests/test_object_store_service.py

@@ -6,7 +6,6 @@ from io import BytesIO
 from unittest import skip
 
 import requests
-import tenacity
 
 from cloudbridge.base import helpers as cb_helpers
 from cloudbridge.interfaces.exceptions import DuplicateResourceException
@@ -211,16 +210,17 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                     raise self.skipTest(
                         "Skipping rest of test - mock providers can't"
                         " access generated url")
-                else:
-                    requests.put(url, data=content)
-
-                for attempt in tenacity.Retrying(stop=tenacity.stop_after_attempt(3),
-                                                 wait=tenacity.wait_fixed(5)):
-                    with attempt:
-                        obj = test_bucket.objects.get(obj_name)
-                        target_stream = BytesIO()
-                        obj.save_content(target_stream)
-                        self.assertEqual(target_stream.getvalue(), content)
+
+                # Only Azure requires the x-ms-blob-type header to be present, but there's no harm
+                # in sending this in for all providers.
+                headers = {'x-ms-blob-type': 'BlockBlob'}
+                response = requests.put(url, headers=headers, data=content)
+                response.raise_for_status()
+
+                obj = test_bucket.objects.get(obj_name)
+                target_stream = BytesIO()
+                obj.save_content(target_stream)
+                self.assertEqual(target_stream.getvalue(), content)
 
     @helpers.skipIfNoService(['storage.buckets'])
     def test_upload_download_bucket_content_from_file(self):