109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
|
|
#
|
|
# Author: Chmouel Boudjnah <chmouel@enovance.com>
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
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
|
|
from urllib import quote as urllib_quote
|
|
|
|
|
|
def quote(value, safe='/'):
|
|
"""
|
|
Patched version of urllib.quote that encodes utf-8 strings before quoting
|
|
"""
|
|
if isinstance(value, unicode):
|
|
value = value.encode('utf-8')
|
|
return urllib_quote(value, safe)
|
|
|
|
|
|
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
|
|
path = quote(path)
|
|
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 %s' % (resp.status, resp.reason))
|
|
|
|
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)
|
|
|
|
|
|
def delete_object(dest_cnx,
|
|
dest_token,
|
|
container_name,
|
|
object_name):
|
|
parsed = dest_cnx[0]
|
|
url = '%s://%s/%s' % (parsed.scheme, parsed.netloc, parsed.path)
|
|
swiftclient.delete_object(url=url,
|
|
token=dest_token,
|
|
container=container_name,
|
|
http_conn=dest_cnx,
|
|
name=object_name)
|
|
|
|
|
|
def sync_object(orig_storage_url, orig_token, dest_storage_url,
|
|
dest_token, container_name, object_name_etag):
|
|
object_name = object_name_etag[1]
|
|
|
|
orig_headers, orig_body = get_object(orig_storage_url,
|
|
orig_token,
|
|
container_name,
|
|
object_name)
|
|
container_name = quote(container_name)
|
|
|
|
post_headers = orig_headers
|
|
post_headers['x-auth-token'] = dest_token
|
|
sync_to = dest_storage_url + "/" + container_name
|
|
swiftclient.put_object(sync_to, name=object_name,
|
|
headers=post_headers,
|
|
contents=_Iter2FileLikeObject(orig_body))
|