Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http://drupal.org/node/469526 by nestor.mata: Support for CloudFiles …
…CDN. AWESOME! :)
- Loading branch information
Showing
20 changed files
with
1,886 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Unless otherwise noted, all files are released under the MIT license, | ||
exceptions contain licensing information in them. | ||
|
||
Copyright (C) 2008 Rackspace US, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
Except as contained in this notice, the name of Rackspace US, Inc. shall not | ||
be used in advertising or otherwise to promote the sale, use or other dealings | ||
in this Software without prior written authorization from Rackspace US, Inc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Cloud Files python client API. | ||
Working with result sets: | ||
>>> import cloudfiles | ||
>>> # conn = cloudfiles.get_connection(username='jsmith', api_key='1234567890') | ||
>>> conn = cloudfiles.get_connection('jsmith', '1234567890') | ||
>>> containers = conn.get_all_containers() | ||
>>> type(containers) | ||
<class 'cloudfiles.container.ContainerResults'> | ||
>>> len(containers) | ||
2 | ||
>>> for container in containers: | ||
>>> print container.name | ||
fruit | ||
vegitables | ||
>>> print container[0].name | ||
fruit | ||
>>> fruit_container = container[0] | ||
>>> objects = fruit_container.get_objects() | ||
>>> for storage_object in objects: | ||
>>> print storage_object.name | ||
apple | ||
orange | ||
bannana | ||
>>> | ||
Creating Containers and adding Objects to them: | ||
>>> pic_container = conn.create_container('pictures') | ||
>>> my_dog = pic_container.create_object('fido.jpg') | ||
>>> my_dog.load_from_file('images/IMG-0234.jpg') | ||
>>> text_obj = pic_container.create_object('sample.txt') | ||
>>> text_obj.write('This is not the object you are looking for.\\n') | ||
>>> text_obj.read() | ||
'This is not the object you are looking for.' | ||
Object instances support streaming through the use of a generator: | ||
>>> deb_iso = pic_container.get_object('debian-40r3-i386-netinst.iso') | ||
>>> f = open('/tmp/debian.iso', 'w') | ||
>>> for chunk in deb_iso.stream(): | ||
.. f.write(chunk) | ||
>>> f.close() | ||
Marking a Container as CDN-enabled/public with a TTL of 30 days | ||
>>> pic_container.make_public(2592000) | ||
>>> pic_container.public_uri() | ||
'http://c0001234.cdn.cloudfiles.rackspacecloud.com' | ||
>>> my_dog.public_uri() | ||
'http://c0001234.cdn.cloudfiles.rackspacecloud.com/fido.jpg' | ||
Set the logs retention on CDN-enabled/public Container | ||
>>> pic_container.log_retention(True) | ||
See COPYING for license information. | ||
""" | ||
|
||
from cloudfiles.connection import Connection, ConnectionPool | ||
from cloudfiles.container import Container | ||
from cloudfiles.storage_object import Object | ||
from cloudfiles.consts import __version__ | ||
|
||
def get_connection(*args, **kwargs): | ||
""" | ||
Helper function for creating connection instances. | ||
@type username: string | ||
@param username: a Mosso username | ||
@type api_key: string | ||
@param api_key: a Mosso API key | ||
@rtype: L{Connection} | ||
@returns: a connection object | ||
""" | ||
return Connection(*args, **kwargs) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
authentication operations | ||
Authentication instances are used to interact with the remote | ||
authentication service, retreiving storage system routing information | ||
and session tokens. | ||
See COPYING for license information. | ||
""" | ||
|
||
import urllib | ||
from httplib import HTTPSConnection, HTTPConnection, HTTPException | ||
from utils import parse_url | ||
from errors import ResponseError, AuthenticationError, AuthenticationFailed | ||
from consts import user_agent, default_authurl | ||
|
||
class BaseAuthentication(object): | ||
""" | ||
The base authentication class from which all others inherit. | ||
""" | ||
def __init__(self, username, api_key, authurl=default_authurl): | ||
self.authurl = authurl | ||
self.headers = dict() | ||
self.headers['x-auth-user'] = username | ||
self.headers['x-auth-key'] = api_key | ||
self.headers['User-Agent'] = user_agent | ||
(self.host, self.port, self.uri, self.is_ssl) = parse_url(self.authurl) | ||
self.conn_class = self.is_ssl and HTTPSConnection or HTTPConnection | ||
|
||
def authenticate(self): | ||
""" | ||
Initiates authentication with the remote service and returns a | ||
two-tuple containing the storage system URL and session token. | ||
Note: This is a dummy method from the base class. It must be | ||
overridden by sub-classes. | ||
""" | ||
return (None, None, None) | ||
|
||
class MockAuthentication(BaseAuthentication): | ||
""" | ||
Mock authentication class for testing | ||
""" | ||
def authenticate(self): | ||
return ('http://localhost/v1/account', None, 'xxxxxxxxx') | ||
|
||
class Authentication(BaseAuthentication): | ||
""" | ||
Authentication, routing, and session token management. | ||
""" | ||
def authenticate(self): | ||
""" | ||
Initiates authentication with the remote service and returns a | ||
two-tuple containing the storage system URL and session token. | ||
""" | ||
conn = self.conn_class(self.host, self.port) | ||
conn.request('GET', self.authurl, '', self.headers) | ||
response = conn.getresponse() | ||
buff = response.read() | ||
|
||
# A status code of 401 indicates that the supplied credentials | ||
# were not accepted by the authentication service. | ||
if response.status == 401: | ||
raise AuthenticationFailed() | ||
|
||
if response.status != 204: | ||
raise ResponseError(response.status, response.reason) | ||
|
||
storage_url = cdn_url = auth_token = None | ||
|
||
for hdr in response.getheaders(): | ||
if hdr[0].lower() == "x-storage-url": | ||
storage_url = hdr[1] | ||
if hdr[0].lower() == "x-cdn-management-url": | ||
cdn_url = hdr[1] | ||
if hdr[0].lower() == "x-storage-token": | ||
auth_token = hdr[1] | ||
if hdr[0].lower() == "x-auth-token": | ||
auth_token = hdr[1] | ||
|
||
conn.close() | ||
|
||
if not (auth_token and storage_url): | ||
raise AuthenticationError("Invalid response from the " \ | ||
"authentication service.") | ||
|
||
return (storage_url, cdn_url, auth_token) | ||
|
||
# vim:set ai ts=4 sw=4 tw=0 expandtab: |
Binary file not shown.
Oops, something went wrong.