softlayer / softlayer-object-storage-python

SoftLayer Object Storage Python Client
Other
19 stars 30 forks source link

Adding headers to uploads #14

Closed SecurityForUs closed 11 years ago

SecurityForUs commented 11 years ago

What's the proper way to add headers to an upload? For example, when I'm creating a manifest it request a couple of headers like X-Object-Manifest (or something to that extent), and there doesn't seem to be any native support in the library for uploading such data type.

SecurityForUs commented 11 years ago

Looking at the storage_object unit test, there's this line: https://github.com/softlayer/softlayer-object-storage-python/blob/master/tests/test_storage_object.py#L19 But wouldn't that cause issues if certain headers (like authentication) is already set?

CrackerJackMack commented 11 years ago

This is how I did it with the backup script. I agree it's missing some functionality in the bindings, something both Kevin and I have talked about recently. See if something like this doesn't work for you.

container = object_storage.get_client(*auth_list)[container_name]
obj = container.storage_object(some_string_literal)
headers = {
        'X-Delete-At': str(when),                                                                                                                  
        'Content-Length': '0'}
obj.make_request('POST', headers=headers)
SecurityForUs commented 11 years ago

While I haven't tested it yet (still finishing up some coding), a friend of mine mentioned doing something like this:

ref = self.object_storage.get_requests_client(<auth stuff>)[container][obj]
headers = {'X-Delete-At' : str('some timestamp here in epoch') }
ref._headers = dict(ref._headers + headers)
ref.create()

This makes it so you can set all the headers you want on creation instead of having to create it then make another request to update the headers. Of course, this is going off of how the referenced link in my previous comment does things.

CrackerJackMack commented 11 years ago

Ah ok, I see what you were wanting. Try this then (minor change):

ref = self.object_storage.get_requests_client(<auth stuff>)[container][obj]
headers = {'X-Delete-At' : str('some timestamp here in epoch') }
ref._headers.update(headers)
ref.create()
SecurityForUs commented 11 years ago

Thought of doing it that way too, heh. Looks like it gives the same results from a short test I did, which is good. Thanks. :+1: Any idea on when better header management might be integrated into the library?

sudorandom commented 11 years ago

I added the ability to pass in arbitrary headers when creating an object and also an 'update' method to issue a POST against the object with arbitrary headers (for updating metadata, X-Delete-At, etc).

https://github.com/softlayer/softlayer-object-storage-python/commit/2d0cc803b709c77056f6a4ba05e2de13fb61d1e5