Last active
December 14, 2015 16:48
-
-
Save chmouel/5117241 to your computer and use it in GitHub Desktop.
Upload between two swifts without touching the local disks.
This file contains hidden or 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
# -*- encoding: utf-8 -*- | |
__author__ = "Chmouel Boudjnah <[email protected]>" | |
from swiftclient import client as swiftclient | |
from swift.common.bufferedhttp import http_connect_raw | |
from swift.common.http import is_success | |
from swift.container.sync import _Iter2FileLikeObject | |
from eventlet import Timeout | |
import urllib2 | |
GET_VM = 'vmm' | |
POST_VM = 'vm' | |
USER = 'admin:admin' | |
PASSWORD = 'password' | |
def get_object(storage_url, token, | |
container_name, | |
object_name, | |
response_timeout=15, | |
conn_timeout=5, | |
resp_chunk_size=65536): | |
headers = {'x-auth-token': token} | |
x = urllib2.urlparse.urlparse(storage_url) | |
path = x.path + '/' + container_name + '/' + object_name | |
with Timeout(conn_timeout): | |
conn = http_connect_raw( | |
x.hostname, | |
x.port, | |
'GET', | |
path, | |
headers=headers, | |
ssl=False) | |
with Timeout(response_timeout): | |
resp = conn.getresponse() | |
if not is_success(resp.status): | |
resp.read() | |
raise swiftclient.ClientException( | |
'status %s' % (resp.status)) | |
if resp_chunk_size: | |
def _object_body(): | |
buf = resp.read(resp_chunk_size) | |
while buf: | |
yield buf | |
buf = resp.read(resp_chunk_size) | |
object_body = _object_body() | |
else: | |
object_body = resp.read() | |
resp_headers = {} | |
for header, value in resp.getheaders(): | |
resp_headers[header.lower()] = value | |
return (resp_headers, object_body) | |
get_url, get_token = swiftclient.Connection( | |
'http://%s:35357/v2.0' % (GET_VM), | |
USER, | |
PASSWORD, auth_version=2).get_auth() | |
container_name = 'foo' | |
object_name = 'foo.iso' | |
get_headers, get_body = get_object(get_url, | |
get_token, | |
container_name, | |
object_name) | |
post_url, post_token = swiftclient.Connection( | |
'http://%s:35357/v2.0' % (POST_VM), | |
USER, | |
PASSWORD, | |
auth_version=2).get_auth() | |
headers = get_headers | |
headers['x-auth-token'] = post_token | |
sync_to = post_url + '/' + container_name | |
print sync_to | |
swiftclient.put_object(sync_to, name=object_name, headers=headers, | |
contents=_Iter2FileLikeObject(get_body)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment