示例#1
0
 def init_app(self, app):
     self._host = app.config.get('BCS_HOST', '')
     access_key = app.config.get('BCS_ACCESS_KEY', '')
     secret_key = app.config.get('BCS_SECRET_KEY', '')
     self._bucket_name = app.config.get('BCS_BUCKET_NAME', '')
     bcs = pybcs.BCS(self._host, access_key, secret_key)
     self._bucket = bcs.bucket(self._bucket_name)
示例#2
0
文件: bcsh.py 项目: hejie/tangmen
def main(args):
    if (args.op == 'auth'):
        auth(args.AK, args.SK, args.BCS_HOST)

    try:
        load_cfg(args)
        bcs = pybcs.BCS(args.BCS_HOST, args.AK, args.SK, pybcs.PyCurlHTTPC)
    except:
        print '''no ak, sk in %s, run bcsh auth --AK=xxx --SK=xxx ''' % get_config_path(
        )
        return

    if (args.op == 'ls'):
        ls(bcs, args.bcs_url, args.detail)
    elif (args.op == 'cat'):
        cat(bcs, args.src)
    elif (args.op == 'download'):
        download(bcs, args.src, args.dst, args.recursive)
    elif (args.op == 'upload'):
        if (args.dry_run):
            global ll__upload, ll__upload_dry
            ll__upload = ll__upload_dry  # replace it
        args.split = parse_size(args.split)
        upload(bcs, args.src, args.dst, args.recursive, args.split)

    elif (args.op == 'rm'):
        if (args.dry_run):
            global __rm, __rm_dry
            __rm = __rm_dry  # replace it
        rm(bcs, args.remote_path, args.recursive)
示例#3
0
    def bcs_save_file3(self):
        """upload file to BCS in BAE3.0"""
        # 保存二进制文件到BCS
        import pybcs
        BCS_HOST = current_app.config.get('BCS_HOST')
        BCS_NAME = current_app.config.get('BCS_NAME')
        BAE_AK = current_app.config.get('BAE_AK')
        BAE_SK = current_app.config.get('BAE_SK')

        bcs = pybcs.BCS(BCS_HOST, BAE_AK, BAE_SK, pybcs.HttplibHTTPC)
        b = bcs.bucket(BCS_NAME)
        o = b.object('/%s' % self.filename.encode('utf-8'))
        result = o.put(self.data)
        time.sleep(1) # 等待文件上传完成
        try:
            assert result['status'] == 200
            return 'http://%s/%s/%s' % (BCS_HOST, BCS_NAME, self.filename)
        except:
            return ''
示例#4
0
def raw(request, key=None):
    if request.META.has_key('HTTP_IF_MODIFIED_SINCE'):
        return HttpResponseNotModified()
    #request.META.get("HTTP_IF_NONE_MATCH", None)
    s = get_object_or_404(Storage, pk=key)
    #read_path =  '/%s/%s/%s'% (s.storage, s.bucket, s.path)
    #image_data = read_gs(read_path)
    tmp = None

    if STORAGE_SERVICE == 'sina':
        import sae.storage
        sc = sae.storage.Client()
        ob = sc.get(STORAGE_BUCKET, s.path)
        url = sc.url(STORAGE_BUCKET, s.path)
        print url
        if ob and ob.data:
            tmp = ob.data

    if STORAGE_SERVICE == 'baidu':
        import pybcs
        bcs = pybcs.BCS('http://bcs.duapp.com/', STORAGE_ACCESS_KEY,
                        STORAGE_SECRET_ACCESS_KEY)
        bucket = bcs.bucket(STORAGE_BUCKET)
        obj_name = u'/%s' % (s.path)
        obj = bucket.object(obj_name.encode('utf8'))
        tmp = obj.get()['body']

    if STORAGE_SERVICE == 'gs':
        src_uri = boto.storage_uri(s.bucket + '/' + s.path, 'gs')
        src_key = src_uri.get_key()
        tmp = tempfile.TemporaryFile()
        src_key.get_file(tmp)
        tmp.seek(0)

    if tmp:
        return cache_response(tmp, s.mime)
    else:
        return HttpResponseNotFound()
示例#5
0
文件: example.py 项目: hejie/tangmen
#!/usr/bin/env python
#coding:utf8

import os
import logging
import pybcs

#设置日志级别
pybcs.init_logging(logging.INFO)

# 请修改这里
AK = '7q6l1grGD7pYFoDG3pEKTxwE'  #请改为你的AK
SK = 'tpTQFy3lK6mKG1yBVGH3lBitwUsfmjhY'  #请改为你的SK
BUCKET = 'bc7q6l1grgd7pyfodg3pektxwembaas'

bcs = pybcs.BCS('http://bcs.duapp.com/', AK, SK,
                pybcs.PyCurlHTTPC)  #这里可以显式选择使用的HttpClient, 可以是:
#HttplibHTTPC
#PyCurlHTTPC
lst = bcs.list_buckets()
print '---------------- list of bucket : '
for b in lst:
    print b
print '---------------- list end'

#声明一个bucket
b = bcs.bucket(BUCKET)

#创建bucket (创建后需要在yun.baidu.com 手动调整quota, 否则无法上传下载)
#b.create()

print b.list_objects_raw(prefix='', start=0, limit=100)
示例#6
0
        for line in f:
            if line[0:6] == 'Serial':
                cpuserial = line[10:26]
        f.close()
    except:
        cpuserial = "ERROR000000000"
    return cpuserial


pybcs.init_logging(logging.INFO)

AK = ''
SK = ''
BUCKET = 'charmyinbucket'

bcs = pybcs.BCS('http://bcs.duapp.com/', AK, SK, pybcs.HttplibHTTPC)

# lst = bcs.list_buckets()
# print '---------------- list of bucket : '
# for b in lst:
# print b
# print '---------------- list end'

#declare bucket
b = bcs.bucket(BUCKET)

print b.get_acl()['body']

#Initiate the camera
camera = picamera.PiCamera()
示例#7
0
def thumbnail(request, key=None):
    if request.META.has_key('HTTP_IF_MODIFIED_SINCE'):
        return HttpResponseNotModified()
    s = get_object_or_404(Storage, pk=key)
    image_data = None

    if STORAGE_SERVICE == 'sina':
        import sae.storage
        sc = sae.storage.Client()
        ob = sc.get(STORAGE_BUCKET, s.path)
        if ob and ob.data:
            image_data = ob.data

    if STORAGE_SERVICE == 'baidu':
        import pybcs
        bcs = pybcs.BCS('http://bcs.duapp.com/', STORAGE_ACCESS_KEY,
                        STORAGE_SECRET_ACCESS_KEY)
        bucket = bcs.bucket(STORAGE_BUCKET)
        obj_name = u'/%s' % (s.path)
        obj = bucket.object(obj_name.encode('utf8'))
        image_data = obj.get()['body']

    if STORAGE_SERVICE == 'gs':
        read_path = '/%s/%s/%s' % (s.storage, s.bucket, s.path)
        #image_data = read_gs(read_path)
        src_uri = boto.storage_uri(s.bucket + '/' + s.path, 'gs')
        src_key = src_uri.get_key()
        tmp = tempfile.TemporaryFile()
        src_key.get_file(tmp)
        tmp.seek(0)
        image_data = tmp
        image_data = tmp.read()

    if image_data:
        # MIN_SIZE = 100
        # image = images.Image(image_data)
        # width = image.width
        # height = image.height
        # if width>height:
        #     rate = width*1.0/height
        # else:
        #     rate = height*1.0/width
        # size = int(MIN_SIZE*rate+1)
        # new_image = images.resize(image_data, width=size, height=size, output_encoding=images.PNG)
        # image = images.Image(new_image)
        # right_x = round(MIN_SIZE*1.0/image.width,5)
        # if right_x>1:
        #     right_x = 1.0
        # else:
        #     left_x = (1- right_x)/2
        #     right_x = right_x + left_x
        # bottom_y = round(MIN_SIZE*1.0/image.height,5)
        # if bottom_y >1:
        #     bottom_y = 1.0
        # else:
        #     top_y = (1-bottom_y)/2
        #     bottom_y = bottom_y + top_y
        # new_image = images.crop(new_image, left_x, top_y, right_x, bottom_y, output_encoding=images.PNG)
        #return cache_response(image_data, s.mime)
        from PIL import Image, ImageOps
        import StringIO
        img = Image.open(StringIO.StringIO(image_data))
        #region = img.resize((100, 100),Image.ANTIALIAS)
        region = ImageOps.fit(img, (100, 100), Image.ANTIALIAS)
        response = HttpResponse(mimetype="image/png")
        format_str = '%a, %d %b %Y %H:%M:%S GMT'
        expires_date = datetime.datetime.utcnow() + datetime.timedelta(365)
        expires_str = expires_date.strftime(format_str)
        last_modified_date = datetime.datetime.utcnow()
        last_modified_str = expires_date.strftime(format_str)
        response['Expires'] = expires_str  #eg:'Sun, 08 Apr 2013 11:11:02 GMT'
        response["Last-Modified"] = last_modified_str  #for 'If-Modified-Since'
        response['Cache-Control'] = 'max-age=172800'

        region.save(response, 'PNG', quality=100)
        return response
    else:
        return HttpResponseNotFound()
示例#8
0
def upload(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            if request.FILES.has_key('images'):
                name = request.FILES['images'].name
                content_type = request.FILES[
                    'images'].content_type or 'application/octet-stream'  #'text/plain'
                file_data = request.FILES['images'].read()

                now = datetime.datetime.now()

                file_ext_pos = name.rfind('.')
                file_name_len = len(name)

                if not (content_type == 'image/jpeg' or content_type
                        == 'image/png' or content_type == 'image/gif'):
                    return
                if file_ext_pos <= 0 and file_ext_pos >= file_name_len:
                    return
                file_ext = name[file_ext_pos - file_name_len:].lower()
                file_uri = '%s%s' % (now.strftime('%Y-%m-%d-%H%M%S-%f'),
                                     file_ext)
                file_name = 'uploads/ohbug/photo/%s%s' % (
                    now.strftime('%Y-%m/%d-%H%M%S-%f'), file_ext)
                file_path = '/%s/%s/%s' % (STORAGE_SERVICE, STORAGE_BUCKET,
                                           file_name)

                if STORAGE_SERVICE == 'sina':
                    import sae.storage
                    s = sae.storage.Client()
                    ob = sae.storage.Object(file_data,
                                            expires='',
                                            content_type=content_type,
                                            content_encoding='gzip')
                    s.put(STORAGE_BUCKET, file_uri, ob)
                    file_name = file_uri

                if STORAGE_SERVICE == 'baidu':
                    # TMPDIR = '/tmp'
                    # try:
                    #     from bae.core import const
                    #     TMPDIR = const.APP_TMPDIR
                    # except Exception, e:
                    #     pass
                    # output = open(TMPDIR+'/'+file_uri, 'wb')
                    # output.write(file_data)
                    # output.close()
                    # try:
                    #   from bae.api import logging as logger
                    # except:
                    #   import logging
                    #   logger = logging.getLogger('pyhttpclient')
                    # logger.info('file_data')
                    # logger.info(file_uri)
                    # BAE API
                    # from bae.api import bcs
                    # baebcs = bcs.BaeBCS('http://bcs.duapp.com/', STORAGE_ACCESS_KEY, STORAGE_SECRET_ACCESS_KEY)
                    # #obj_path = TMPDIR+'/'+file_uri
                    # obj_name = u'/%s'%(file_uri)
                    # baebcs.put_object(STORAGE_BUCKET, obj_name.encode('utf8'), file_data)
                    # #baebcs.put_file(STORAGE_BUCKET, obj_name.encode('utf8'), obj_path.encode('utf8'))
                    # baebcs.make_public(STORAGE_BUCKET, obj_name.encode('utf8'))

                    # BCS API
                    import pybcs
                    # # TMPDIR = '/tmp'
                    # # try:
                    # #     from bae.core import const
                    # #     TMPDIR = const.APP_TMPDIR
                    # # except Exception, e:
                    # #     pass
                    # # output = open(TMPDIR+'/'+file_uri, 'wb')
                    # # output.write(file_data)
                    # # output.close()
                    bcs = pybcs.BCS('http://bcs.duapp.com/',
                                    STORAGE_ACCESS_KEY,
                                    STORAGE_SECRET_ACCESS_KEY)
                    bucket = bcs.bucket(STORAGE_BUCKET)
                    obj_name = u'/%s' % (file_uri)
                    obj = bucket.object(obj_name.encode('utf8'))
                    obj.put(file_data)
                    obj.make_public()
                    #obj.put_file(TMPDIR+'/'+file_uri)
                    file_name = file_uri

                # Google Storage
                if STORAGE_SERVICE == 'gs':
                    dst_uri = boto.storage_uri(STORAGE_BUCKET, STORAGE_SERVICE)

                    new_dst_uri = dst_uri.clone_replace_name(file_name)
                    #logging.info(dst_uri)
                    #logging.info(new_dst_uri)
                    tmp = tempfile.TemporaryFile()
                    tmp.write(file_data)
                    tmp.seek(0)
                    dst_key = new_dst_uri.new_key()
                    dst_key.content_type = content_type
                    dst_key.set_contents_from_file(tmp)
                    #logger.info('hello')

                    # write_path = files.gs.create(file_path, acl='bucket-owner-full-control',mime_type=content_type)
                    # with files.open(write_path, 'a') as fp:
                    #     fp.write(file_data)
                    # files.finalize(write_path)

                s = Storage()
                s.storage = STORAGE_SERVICE
                s.bucket = STORAGE_BUCKET
                s.path = file_name
                s.mime = content_type
                s.size = len(file_data)
                s.md5 = hashlib.md5(file_data).hexdigest()
                s.name = name
                s.kind = 'image'
                s.author = request.user
                s.save()
                return HttpResponseRedirect('/photo/%s' % s.key)
    else:
        form = UploadForm()
    return render_to_response('storage/upload.html', {'form': form},
                              context_instance=RequestContext(request))
示例#9
0
文件: settings.py 项目: zky001/me
    pass

#####################################
# Image Upload
#####################################
if RUNTIME_ENV in ("bae", ):
    import pybcs

    pybcs_client = pybcs.HttplibHTTPC
    try:
        import pycurl
        pybcs_client = pybcs.PyCurlHTTPC
    except:
        pass

    BAE_BCS = pybcs.BCS(const.BCS_ADDR, const.BCS_USER, const.BCS_PASS,
                        pybcs_client)
    BAE_BUCKET = BAE_BCS.bucket(const.BCS_BUCKET)

elif RUNTIME_ENV in ("sae", ):
    from sae.storage import Bucket

    SAE_BUCKET = Bucket(const.SAE_BUCKET)

elif RUNTIME_ENV in ("local", ):
    UPLOAD_URL = "static/uploads/"
    UPLOAD_FOLDER = os.path.join(app.root_path, UPLOAD_URL)

elif RUNTIME_ENV in ("gae", "gae_dev"):
    BLOB_SERVING_URL = "/_files"
    BLOB_UPLOAD_URL = "/_upload"
    app.config["BLOB_SERVING_URL"] = BLOB_SERVING_URL
示例#10
0
import logging

sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + "/..")

import pybcs
#pybcs.init_logging(logging.ERROR, True, log_file_path=None)
pybcs.init_logging(logging.INFO, True, log_file_path=None)
#pybcs.init_logging(logging.DEBUG, True, log_file_path=None)

HOST = 'http://bs.baidu.com'

AK = 'gNQ17x3aoMs8'
SK = 'HWmtE17aWcg8sK2fn4B1J3w8vMm'
BUCKET = 'p-59b805098a2c627624741539e70eb52e'
OBJECT = '/009cf012ece59d79afbf3e73d9af739a'

bcs1 = pybcs.BCS(HOST, AK, SK, pybcs.HttplibHTTPC)
bcs2 = pybcs.BCS(HOST, AK, SK)
for bcs in [bcs1, bcs2]:
    b = bcs.bucket(BUCKET)
    o = b.object(OBJECT)
    try:
        o.get_to_file('xxxxxxx', headers={'Range': 'bytes=414449660-'})
    except Exception, e:
        print 'Exception:\n', e

    try:
        o.get(headers={'Range': 'bytes=414449660-'})
    except Exception, e:
        print 'Exception:\n', e
示例#11
0
 def __init__(self, URL, AK, SK, BUCKET,EXCEPT):
     bcs = pybcs.BCS(URL, AK, SK, pybcs.HttplibHTTPC)
     self.bucket = bcs.bucket(BUCKET)
     self.excepts = EXCEPT
示例#12
0
from django.utils import simplejson
from urllib import unquote

global isReleaseVersion
isReleaseVersion = 'SERVER_SOFTWARE' in os.environ
SYSTEM_ENCODING = isReleaseVersion and 'utf8' or 'gbk'

import logging
import pybcs

BCS_HOST = 'bcs.duapp.com'
AK = 'kcuOUo51MNqEwAFGEVOGxvWy'
SK = 'AuDUW107HmqOhDFzMqHMfEtFeshyZcmB'

global baebcs
baebcs = pybcs.BCS(BCS_HOST, AK, SK, pybcs.HttplibHTTPC)

SYS_UPLOAD_DIRS = os.path.join(os.path.dirname(__file__),
                               '../upload_files/sys/').replace('\\', '/')
CAD_UPLOAD_DIRS = os.path.join(os.path.dirname(__file__),
                               '../upload_files/cad/').replace('\\', '/')
SHX_EXT = u'.shx'
CAD_FONT = u'cad'
SYS_EXT = [u'.ttf', u'.otf']


def log(message):
    with open(
            os.path.join(os.path.dirname(__file__),
                         '../static/exception.log').replace('\\', '/'),
            'a') as file:
示例#13
0
 def test_1(self):
     print 'test with pybcs.HttplibHTTPC'
     bcs = pybcs.BCS(HOST, AK, SK, pybcs.HttplibHTTPC)
     self._run_test(bcs, BUCKET)
示例#14
0
#coding=utf8
import pybcs
import logging
from tempfile import NamedTemporaryFile

#pybcs.init_logging(logging.INFO)

AK = ''           #请改为你的AK
SK = ''         #请改为你的SK

BUCKET='myimage1'

bcs = pybcs.BCS('http://bcs.duapp.com/', AK, SK)

def dump_file(filename,file_obj):
    b = bcs.bucket(BUCKET)
    with NamedTemporaryFile() as f:
        o = b.object('/%s'%filename)
        o.put_file(f.name)

def read_file(filename):
    b = bcs.bucket(BUCKET)
    with NamedTemporaryFile() as f:
        o = b.object('/%s'%filename)
        o.get_to_file(f.name)
        return True,f.read()