Beispiel #1
0
    def test_encryption_multipart_upload(self):
        import math, os
        from ks3.encryption import Crypts
        Crypts.generate_key('D:/', 'key.txt')
        c = Connection(ak,
                       sk,
                       host="",
                       is_secure=False,
                       domain_mode=False,
                       local_encrypt=True,
                       local_key_path="D:/key.txt")
        from filechunkio import FileChunkIO
        bucket = c.get_bucket(test_bucket)

        source_path = 'D:/1.exe'
        source_size = os.stat(source_path).st_size
        mp = bucket.initiate_multipart_upload(os.path.basename(source_path),
                                              calc_encrypt_md5=False)
        chunk_size = 5242880
        chunk_count = int(math.ceil(source_size // chunk_size))
        print(chunk_count)
        for i in range(chunk_count + 1):
            offset = chunk_size * i
            last = False
            bytes = min(chunk_size, source_size - offset)
            if i == chunk_count + 1:
                last = True
            with FileChunkIO(source_path, 'r', offset=offset,
                             bytes=bytes) as fp:
                mp.upload_part_from_file(fp, part_num=i + 1, is_last_part=last)

        mp.complete_upload()
Beispiel #2
0
class OSS(object):
    def __init__(self, ak, sk, region):
        self.c = Connection(ak, sk, host=region)
        self.buckets = self.c.get_all_buckets()

    def print_all_buckets(self):
        for b in self.buckets:
            print(b.name)

    def print_key_name(self, bucket_name):
        b = self.c.get_bucket(bucket_name)
        for k in b.list():
            print(k.name)

    def create_bucket(self):
        raise NotImplementedError

    def get_md5(self, filename):
        with open(filename, 'rb') as f:
            md5 = hashlib.md5()
            md5.update(f.read())
        #return (md5.digest(), md5.digest().encode('base64')[:-1])
        return md5

    def upload(self, pathname, bucket_name, policy="public-read"):
        """upload file or dir to specified bucket"""
        b = self.c.get_bucket(bucket_name)
        if os.path.isfile(pathname):
            key_name = os.path.basename(pathname)
            k = b.new_key(key_name)
            md5 = self.get_md5(pathname)
            print(pathname)
            print(key_name)
            k.set_contents_from_filename(
                pathname,
                policy=policy,
                md5=(md5.digest(), md5.digest().encode('base64')[:-1]))
        elif os.path.isdir(pathname):
            basename = os.path.basename(pathname)
            print(basename)
            for root, dirs, files in os.walk(pathname):
                for f in files:
                    filename = os.path.join(root, f)
                    key_name = "%s/%s" % (
                        basename, filename.replace(pathname, "").replace(
                            os.path.sep, '/').lstrip('/'))

                    k = b.new_key(key_name)
                    md5 = self.get_md5(filename)
                    print(filename)
                    print(key_name)
                    k.set_contents_from_filename(
                        filename,
                        policy=policy,
                        md5=(md5.digest(), md5.digest().encode('base64')[:-1]))
Beispiel #3
0
 def test_encryption_upload(self):
     from ks3.encryption import Crypts
     Crypts.generate_key('D:/', 'key.txt')
     c = Connection(ak,
                    sk,
                    host="",
                    is_secure=False,
                    domain_mode=False,
                    local_encrypt=True,
                    local_key_path="D:/key.txt")
     b = c.get_bucket(test_bucket)
     #put
     kw = b.new_key(test_key)
     ret = kw.set_contents_from_string("some thing")
     #get
     get_key = b.get_key(test_key)
     s = get_key.get_contents_as_string()
     print("Result:", s)
class Conn:
    def __init__(self, ak, sk, host, bucket):
        self.bucket = bucket
        self.ksyun = Connection(ak, sk, host=host)
        if not self._exist(bucket):
            self.ksyun.create_bucket(bucket)
        assert self._exist(bucket)

    def _exist(self, bucket):
        buckets = [b.name for b in self.ksyun.get_all_buckets()]
        return bucket in buckets

    def upload(self, keyname, filename):
        try:
            bucket = self.ksyun.get_bucket(self.bucket)
            key_object = bucket.new_key(keyname)
            key_object.set_contents_from_filename(filename)
            return {"result": 0, "msg": keyname}
        except:
            return {"result": -1, "msg": traceback.format_exc()}
            pass

    def download(self, filename, keyname):
        try:
            bucket = self.ksyun.get_bucket(self.bucket)
            key_object = bucket.get_key(keyname)
            key_object.get_contents_to_filename(filename)
            return {"result": 0, "msg": filename}
        except:
            return {"result": -1, "msg": traceback.format_exc()}
            pass

    def make_key(self, app_key, version, package_name):
        return ''.join([
            app_key,
            md5.new(version).hexdigest(),
            md5.new(package_name).hexdigest()
        ]).lower()
Beispiel #5
0
from ks3.connection import Connection
import os

## Nice ak/sk
#ak = 'TnivEIMCaLje6fGL20oK'
#sk = 'lmppx27iWiOOxiMIwUe4HYldLmrPb0aHTIafYf53'

## GIf ak/sk
ak = 'Sb1VoKVZsflmAP2hle8l'
sk = 'JRpqvaDOeKA279PYWulIWeBcfVUHUFsJUhkS6ozo'

#kss.ksyun.com hangzhou bucket domain
#c = Connection(ak, sk, host="kss.ksyun.com")

#kss.ksyun.com beijing bucket domain
c = Connection(ak, sk, host="ks3-cn-beijing.ksyun.com")

bucket_name = "kuaishou-hls"
#key_name = "record/live/111/hls/111.m3u8"
#stra="#EXT-X-TARGETDURATION:5"
#strb="#EXT-X-TARGETDURATION:15\n"

b = c.get_bucket(bucket_name)
'''
def change_duration(file):
	fh = open("/tmp/1/"+file,'w+')
	#fh.readlines()
#	lines = open("/tmp/1/"+file,'w+').readlines()
	for i in fh.readlines():
		print i
	#print lines
Beispiel #6
0
# @File    : ks_upload_file_stream.py
# @Software: PyCharm
# from __future__ import print_function
import time
import os
import hashlib
# from toolbox.Hash import file_hash
from logger import get_logger, func_time_logger
from ks3.connection import Connection

logger = get_logger('ks_uploader')

ak = 'K0fJOlnF++ck5LznhuNZ'
sk = 'o4D5wjs6r02dxLDLyLbTTUenTvpmKgrBItra6qgb'

connection = Connection(ak, sk, host='kss.ksyun.com')


def get_stream_md5(stream):
    stream.seek(0)
    hash_md5 = hashlib.md5()
    for chunk in iter(lambda: stream.read(4096), b""):
        hash_md5.update(chunk)
    stream.seek(0)
    return hash_md5.hexdigest()


@func_time_logger
def upload_ks_file_stream(bucket_name,
                          upload_key,
                          file_obj,
Beispiel #7
0
#coding:utf-8
import unittest
import pytest

from ks3.auth import add_auth_header
from ks3.connection import Connection
from ks3.bucket import Bucket
from ks3.key import Key
from ks3.acl import Policy, ACL, Grant
from ks3.user import User

ak = 'YOUR_ACCESS_KEY'
sk = 'YOUR_SECRET_KEY'
conn = Connection(ak, sk, host="")
test_bucket = 'sdktestt'
test_key = 'test_key'


# ------------------------------------Auth relative test------------------------------
class TestAuthToken(unittest.TestCase):
    def test_auth_token(self):
        headers = {}
        method = "POST"
        bucket = test_bucket
        key = test_key
        query_args = {'upload': None}
        add_auth_header(ak, sk, headers, method, bucket, key, query_args)
        print(headers['Authorization'])


# ------------------------------------bucket relative test------------------------------
Beispiel #8
0
#!/bin/python
# -*- coding: UTF-8 -*-

from ks3.connection import Connection
ak = 'TnivEIMCaLje6fGL20oK'
sk = 'lmppx27iWiOOxiMIwUe4HYldLmrPb0aHTIafYf53'

c = Connection(ak, sk, host="kss.ksyun.com")

my_bucket = "vod"
buckets = c.get_all_buckets()

b = c.get_bucket(my_bucket)

keys = b.list()
acp = b.get_acl()
print acp
for k in keys:
	print k
'''
for b in buckets:
	#print b.name
	if b.name = my_bucket:
		acp = my_bucket.get_acl()
		print acp.acl.grant.permission


#acp = sqtest.get_acl()
#for grant in acp.acl.grants:
#print acp.acl.grant.permission
#print grant.permission, grant.display_name, grant.email_address, grant.id
Beispiel #9
0
import unittest
import pytest

from ks3.auth import add_auth_header
from ks3.connection import Connection
from ks3.bucket import Bucket
from ks3.key import Key
from ks3.acl import Policy, ACL, Grant
from ks3.user import User

ak = 'YOUR_ACCESS_KEY'
sk = 'YOUR_SECRET_KEY'
#conn = Connection(ak, sk, server='test.ks3.ksyun.com')
conn = Connection(ak, sk)
test_bucket = 'sdktest123'
test_key = 'test_key'


#------------------------------------Auth relative test------------------------------
class TestAuthToken(unittest.TestCase):
    def test_auth_token(self):
        headers = {}
        method = "POST"
        bucket = test_bucket
        key = test_key
        query_args = {'upload': None}
        add_auth_header(ak, sk, headers, method, bucket, key, query_args)
        print(headers['Authorization'])


#------------------------------------bucket relative test------------------------------
Beispiel #10
0
 def __connect_ks(self):
     ksConnect = Connection(self.__access_key, self.__secret_key, host=self.__endpoint)
     bucket = ksConnect.get_bucket(self.__bucket)
     return bucket
Beispiel #11
0
def make_cli(ak, sk, endpoint):
    return Connection(ak,
                      sk,
                      host=endpoint,
                      is_secure=False,
                      domain_mode=False)
Beispiel #12
0
 def __init__(self, ak, sk, region):
     self.c = Connection(ak, sk, host=region)
     self.buckets = self.c.get_all_buckets()
 def __init__(self, ak, sk, host, bucket):
     self.bucket = bucket
     self.ksyun = Connection(ak, sk, host=host)
     if not self._exist(bucket):
         self.ksyun.create_bucket(bucket)
     assert self._exist(bucket)
Beispiel #14
0
else:
    ak = sys.argv[1]
    sk = sys.argv[2]
    bucket_name = sys.argv[3]
    publish_dir = sys.argv[4]
    remote_dir = sys.argv[5]
    cdn_url = sys.argv[6]

print "bucket_name: " + bucket_name
print "publish_dir: " + publish_dir
print "remote_dir: " + remote_dir
print "cdn_url: " + cdn_url

from ks3.connection import Connection

connection = Connection(ak, sk, host=ks3_host, is_secure=True, domain_mode=False)
bucket = connection.get_bucket(bucket_name)


def upload_file(key_name, filename):
    try:
        key = bucket.new_key(key_name)
        ret = key.set_contents_from_filename(filename, policy="public-read")
        if ret and ret.status == 200:
            return True
    except:
        pass
        return False


def upload_dir(x, dir_name, files):