Home > openstack, swift > Change Default Response of Swift GET Request

Change Default Response of Swift GET Request

Swift is the object storage of OpenStack. Python-SwiftClient is the default client program to interact with OpenStack Swift Storage. In this note along with python-swiftclient I would also use ‘curl’ command occasionally. 

The objective of this note, is to change / modify the content of the received file with  swift GET request. To illustrate, lets assume I have a file (foo.txt) in a container named ‘bar’ inside swift storage.  ‘foo.txt’ contain following following line “You are who you are!”. So, if we send a GET request for ‘foo.txt’ we would get the original content “You are who you are”. But here, I would illustrate, how you can change swift-objectserver so that instead of the original content you would receive ‘yes, you are smart’. Why you need this – I won’t tell you. Find your own use case.

So, this is what happens:

Swift Default behavior

What we want to achive:

Desired output

How we do it:

In my environment, I have following token and Storage url set up. So that my curl command connect to the right endpoint with valid token.

Token and Storage URL

Now, I would point out changes required:

Now, let me explain how I can do this change.

Swift Object server is responsible to read requested file from disk and pass the file content back to the swift proxy server.

The actual reading of the requested file, is done in this   line (see github) or shown here below:

response = Response(
request=request, conditional_response=True)

In this case, the Response Class, build the response for the given request. ‘app_iter’ has the iterator of the disk file as shown in the file. 

But Instead of ‘app_iter’, we can also give the actual ‘body’ of the request. See, the definition of  Request Class for more information. 

If you look into the following figure, we are commenting out ‘app_iter’ and giving ‘body’ with our desired value. 

Changes in the Swift code 

The next thing what we need, is to set the content length which is set by the ‘obj_size’ variable. Which I am setting next with len(response.body).

Boom! Now, restart you object server. And  send the request for the file.

Categories: openstack, swift Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: