Esempio n. 1
0
 def test_overwrite_cn(self):
     objname = to_unicode("本地文件名.txt")
     logging.info("objname: %s", objname)
     with self.save_file(objname, content=u'我的座右铭') as name_1:
         self.assertEqual(name_1, objname)
         handle = default_storage.open(name_1)
         self.assertEqual(to_unicode(handle.read()), '我的座右铭')
     with self.save_file(objname, content=u'这是一个测试') as name_2:
         self.assertEqual(name_2, objname)
         handle = default_storage.open(name_2)
         self.assertEqual(to_unicode(handle.read()), '这是一个测试')
Esempio n. 2
0
 def test_url_cn(self):
     objname = to_unicode("本地文件名.txt")
     logging.info("objname: %s", objname)
     with self.save_file(objname, content=u'我的座右铭') as name:
         self.assertEqual(name, objname)
         url = default_storage.url(objname, 300)
         logging.info("url: %s", url)
         response = requests.get(url)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(to_unicode(response.content), '我的座右铭')
         self.assertEqual(response.headers['Content-Type'], "text/plain")
Esempio n. 3
0
    def fetch_sts_token(access_key_id, access_key_secret, role_arn):
        """子用户角色扮演获取临时用户的密钥
        :param access_key_id: 子用户的 access key id
        :param access_key_secret: 子用户的 access key secret
        :param role_arn: STS角色的Arn
        :return StsToken: 临时用户密钥
        """
        clt = client.AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')
        req = AssumeRoleRequest.AssumeRoleRequest()

        req.set_accept_format('json')
        req.set_RoleArn(role_arn)
        req.set_RoleSessionName('oss-python-sdk-example')

        body = clt.do_action_with_exception(req)

        j = json.loads(oss2.to_unicode(body))

        token = StsToken()

        token.access_key_id = j['Credentials']['AccessKeyId']
        token.access_key_secret = j['Credentials']['AccessKeySecret']
        token.security_token = j['Credentials']['SecurityToken']
        token.request_id = j['RequestId']
        token.expiration = oss2.utils.to_unixtime(
            j['Credentials']['Expiration'], '%Y-%m-%dT%H:%M:%SZ')

        return token
def fetch_sts_token(access_key_id, access_key_secret, role_arn):
    """子用户角色扮演获取临时用户的密钥
    :param access_key_id: 子用户的 access key id
    :param access_key_secret: 子用户的 access key secret
    :param role_arn: STS角色的Arn
    :return StsToken: 临时用户密钥
    """
    clt = client.AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')
    req = AssumeRoleRequest.AssumeRoleRequest()

    req.set_accept_format('json')
    req.set_RoleArn(role_arn)
    req.set_RoleSessionName('oss-python-sdk-example')

    body = clt.do_action_with_exception(req)

    j = json.loads(oss2.to_unicode(body))

    token = StsToken()

    token.access_key_id = j['Credentials']['AccessKeyId']
    token.access_key_secret = j['Credentials']['AccessKeySecret']
    token.security_token = j['Credentials']['SecurityToken']
    token.request_id = j['RequestId']
    token.expiration = oss2.utils.to_unixtime(j['Credentials']['Expiration'], '%Y-%m-%dT%H:%M:%SZ')

    return token
Esempio n. 5
0
def upload():
    print("**********   上传  *******")
    # 把本地文件 “座右铭.txt” 上传到OSS,新的Object叫做 “我的座右铭.txt”
    # 注意到,这次put_object()的第二个参数是file object;而上次上传是一个字符串。
    # put_object()能够识别不同的参数类型
    bucket_input = raw_input('请输入要传入的bucket名:   ')
    print("**************************")
    print("     当前目录下所有文件:")
    for file in dirs:
        print(file)
    print("***************************")

    filename = raw_input('请输入要上传的文件名: ')
    cloud_name = raw_input('请输入云端文件名:   ')
    bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret),
                         endpoint,
                         bucket_name=bucket_input)
    with open(oss2.to_unicode(filename), 'rb') as f:
        bucket.put_object(cloud_name, f)
    meta = bucket.get_object_meta(cloud_name)
    if meta:
        print("     上传成功")
        print("     云端所有文件:")
        for i in oss2.ObjectIterator(bucket):
            print(i.key)

    else:
        print("     上传失败")
Esempio n. 6
0
def uploadDir(folderPath_: str, resFolderPath_: str, bucket_, removeTargetFolderPath_: str):
    _filePaths = os.listdir(folderPath_)
    for _filePath in _filePaths:
        _path = folderPath_ + '/' + _filePath
        if os.path.isdir(_path):
            uploadDir(_path, resFolderPath_, bucket_, removeTargetFolderPath_)
        else:
            _remoteFilePath = removeTargetFolderPath_ + "/res" + _path.split(resFolderPath_).pop()
            '''
            exist = bucket_.object_exists(_remoteFilePath)
            # 返回值为true表示文件存在,false表示文件不存在。
            if exist:
                print("    " + str(_remoteFilePath) + " 已存在 -")
            else:
                with open(oss2.to_unicode(_path), 'rb') as _file:
                    bucket_.put_object(_remoteFilePath, _file)
                _meta = bucket_.get_object_meta(_remoteFilePath)
                if _meta:
                    print("    " + str(_remoteFilePath) + " 上传成功 +")
                else:
                    print("    " + str(_remoteFilePath) + " 上传失败 x")
            '''
            # 获取文件位置,然后内容放置于其内
            with open(oss2.to_unicode(_path), 'rb') as _file:
                bucket_.put_object(_remoteFilePath, _file)
            # 判断文件是否正确放置(正确放置了之后,是可以取到的)。
            _meta = bucket_.get_object_meta(_remoteFilePath)
Esempio n. 7
0
    def upload(self, path_direct):
        print("**********   上传  *******")
        bucket_input = input('请输入要传入的bucket名:   ')
        print("**************************")
        print("    上传目录下所有文件:")
        dirs = os.listdir(path_direct)
        for file in dirs:
            print(file)
        print("***************************")

        filename = input('请输入要上传的文件名: ')
        cloud_name = input('请输入云端文件名:   ')
        bucket = oss2.Bucket(oss2.Auth(self.access_key_id,
                                       self.access_key_secret),
                             self.endpoint,
                             bucket_name=bucket_input)
        with open(oss2.to_unicode(filename), 'rb') as f:
            bucket.put_object(cloud_name, f)
        meta = bucket.get_object_meta(cloud_name)
        if meta:
            print("     上传成功")
            print("     云端所有文件:")
            for i in oss2.ObjectIterator(bucket):
                print(i.key)
        else:
            print("     上传失败")
Esempio n. 8
0
def update_file(bucket, local_file_path, local_filename, cloud_file_path,
                cloud_filename):
    statinfo = os.stat(local_file_path + local_filename)
    #注意时区的转换
    # local_last_modified_time=time.mktime(time.localtime(statinfo.st_mtime))-28800#统一转换为以秒为单位,北京时间-8小时=GMT
    local_last_modified_time = int(
        statinfo.st_mtime) - 28800  #统一转换为以秒为单位,北京时间-8小时=GMT
    with open(oss2.to_unicode(local_file_path + local_filename), 'rb') as f:
        if bucket.object_exists(cloud_file_path + cloud_filename):  #云端已经存在该文件
            cloud_last_modified_time = date_to_num(
                bucket.get_object(cloud_file_path + cloud_filename))
            # print(local_last_modified_time,cloud_last_modified_time)
            if local_last_modified_time > cloud_last_modified_time:
                #如果本地的文件较新才更新到云端
                print('[update] ' + cloud_file_path + cloud_filename + '.')
                bucket.put_object(cloud_file_path + cloud_filename,
                                  f)  #将本地文件更新到云端cloud_file_path+cloud_filename
            else:
                print('[scan] ' + local_file_path + local_filename + '.')
                print(cloud_filename + ' already up-to-date.')
        else:
            print(cloud_filename + ' does not exist,start upload...')
            print('[upload] ' + local_file_path + local_filename + '.')
            bucket.put_object(cloud_file_path + cloud_filename,
                              f)  #将本地文件上传到云端cloud_file_path+cloud_filename
Esempio n. 9
0
def download_file(osspath, localpath):
    bucket = get_bucket()
    try:
        os.path.join(localpath)
    except OSError:
        pass
    with open(oss2.to_unicode(localpath), 'wb') as f:
        shutil.copyfileobj(bucket.get_object(osspath), f)
Esempio n. 10
0
 def __check(self, image_key, image_height, image_width, image_size, image_format):
     result = self.bucket.get_object(image_key, process='image/info')
     json_content = result.read()
     decoded_json = json.loads(oss2.to_unicode(json_content))
     
     self.assertEqual(int(decoded_json['ImageHeight']['value']), image_height)
     self.assertEqual(int(decoded_json['ImageWidth']['value']), image_width)
     self.assertEqual(int(decoded_json['FileSize']['value']), image_size)
     self.assertEqual(decoded_json['Format']['value'], image_format)
 def __check(self, image_key, image_height, image_width, image_size, image_format):
     result = self.bucket.get_object(image_key, process='image/info')
     json_content = result.read()
     decoded_json = json.loads(oss2.to_unicode(json_content))
     
     self.assertEqual(int(decoded_json['ImageHeight']['value']), image_height)
     self.assertEqual(int(decoded_json['ImageWidth']['value']), image_width)
     self.assertEqual(int(decoded_json['FileSize']['value']), image_size)
     self.assertEqual(decoded_json['Format']['value'], image_format)
Esempio n. 12
0
    def getimg(self):
        key = 'example.jpg'
        result = self.bucket.get_object(key, process='image/info')

        json_content = result.read()
        decoded_json = json.loads(oss2.to_unicode(json_content))
        assert int(decoded_json['ImageHeight']['value']) == 267
        assert int(decoded_json['ImageWidth']['value']) == 400
        assert int(decoded_json['FileSize']['value']) == 21839
        assert decoded_json['Format']['value'] == 'jpg'
Esempio n. 13
0
def upload(filename: object, cloud_name: object) -> object:
    auth = oss2.Auth(myAccessKeyId, myAccessKeySecret)
    bucket = oss2.Bucket(auth, EndPoint, myBucketName)
    with open(oss2.to_unicode(filename), 'rb') as f:
        bucket.put_object(cloud_name, f)
    meta = bucket.get_object_meta(cloud_name)
    print(meta)
    if meta:
        return myBucketUrl + cloud_name
    else:
        return ''
    def get_sts(self):
        clt = client.AcsClient(OSS_STS_ID, OSS_STS_KEY, OSS_REGION)
        req = AssumeRoleRequest.AssumeRoleRequest()

        req.set_accept_format('json')
        req.set_RoleArn(OSS_STS_ARN)
        req.set_RoleSessionName('oss-python-sdk-example')

        body = clt.do_action_with_exception(req)

        j = json.loads(oss2.to_unicode(body))

        return j['Credentials']['AccessKeyId'], j['Credentials']['AccessKeySecret'], j['Credentials']['SecurityToken']
Esempio n. 15
0
    def uploadFile(self,cloud_name,filename):
        def percentage(consumed_bytes, total_bytes):
            if total_bytes:
                rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
                self.communicate_2.emit(rate)
                print('\r{0}% '.format(rate), end='')

        with open(oss2.to_unicode(filename), 'rb') as f:
            OSS.bucket.put_object(cloud_name, f,progress_callback=percentage)
        meta =  OSS.bucket.get_object_meta(cloud_name)
        if meta:
            return True
        else:
            return False
    def get_sts(self):
        clt = client.AcsClient(OSS_STS_ID, OSS_STS_KEY, OSS_REGION)
        req = AssumeRoleRequest.AssumeRoleRequest()

        req.set_accept_format('json')
        req.set_RoleArn(OSS_STS_ARN)
        req.set_RoleSessionName('oss-python-sdk-example')

        body = clt.do_action_with_exception(req)

        j = json.loads(oss2.to_unicode(body))

        return j['Credentials']['AccessKeyId'], j['Credentials'][
            'AccessKeySecret'], j['Credentials']['SecurityToken']
Esempio n. 17
0
    def test_overwrite_cn(self):
        objname = to_unicode("本地文件名.txt")
        logging.info("objname: %s", objname)
        data = oss2.compat.to_bytes('我的座右铭')
        with self.save_file(objname, content=data) as name_1:
            self.assertEqual(name_1, objname)
            handle = default_storage.open(name_1)
            self.assertEqual(handle.read(), data)

        data = oss2.compat.to_bytes('这是一个测试')
        with self.save_file(objname, content=data) as name_2:
            self.assertEqual(name_2, objname)
            handle = default_storage.open(name_2)
            self.assertEqual(handle.read(), data)
Esempio n. 18
0
def put_object_png(script_identity="", png=""):
    """
    上传图片
    :param script_identity:
    :param png:
    :return:
    """
    date = datetime.now()
    now = date.strftime("%Y%m%d%H%M%S")
    # 设置meta信息
    oss_bucket_path = "png/{}/{}_{}.png".format(script_identity,
                                                script_identity, now)
    with open(oss2.to_unicode(png), 'rb') as fs:
        bucket.put_object(oss_bucket_path, fs)
    return bucket.sign_url('GET', oss_bucket_path, 60 * 5)
    def test_compat(self):
        # from unicode
        u = u'中文'

        self.assertEqual(u, oss2.to_unicode(u))
        self.assertEqual(u.encode('utf-8'), oss2.to_bytes(u))

        if is_py2:
            self.assertEqual(u.encode('utf-8'), oss2.to_string(u))

        if is_py3:
            self.assertEqual(u, oss2.to_string(u))

        # from bytes
        b = u.encode('utf-8')

        self.assertEqual(b.decode('utf-8'), oss2.to_unicode(b))
        self.assertEqual(b, oss2.to_bytes(b))

        if is_py2:
            self.assertEqual(b, oss2.to_string(b))

        if is_py3:
            self.assertEqual(b.decode('utf-8'), oss2.to_string(b))
    def test_compat(self):
        # from unicode
        u = u'中文'

        self.assertEqual(u, oss2.to_unicode(u))
        self.assertEqual(u.encode('utf-8'), oss2.to_bytes(u))

        if is_py2:
            self.assertEqual(u.encode('utf-8'), oss2.to_string(u))

        if is_py3:
            self.assertEqual(u, oss2.to_string(u))

        # from bytes
        b = u.encode('utf-8')

        self.assertEqual(b.decode('utf-8'), oss2.to_unicode(b))
        self.assertEqual(b, oss2.to_bytes(b))

        if is_py2:
            self.assertEqual(b, oss2.to_string(b))

        if is_py3:
            self.assertEqual(b.decode('utf-8'), oss2.to_string(b))
Esempio n. 21
0
 def uploadDir(self, folderPath_: str):
     _filePaths = os.listdir(folderPath_)
     for _filePath in _filePaths:
         _path = folderPath_ + '/' + _filePath
         if os.path.isdir(_path):
             self.uploadDir(_path)
         else:
             _remoteFilePath = self.targetFolderPath + _path.split(
                 self.resPath).pop()
             with open(oss2.to_unicode(_path), 'rb') as _file:
                 self.bucket.put_object(_remoteFilePath, _file)
             _meta = self.bucket.get_object_meta(_remoteFilePath)
             if _meta:
                 print(str(_remoteFilePath) + " 上传成功 +")
             else:
                 print(str(_remoteFilePath) + " 上传失败 xXx")
Esempio n. 22
0
def fetch_sts_token(access_key_id, access_key_secret, role_arn):
    clt = client.AcsClient(access_key_id, access_key_secret, "cn-hangzhou")
    req = AssumeRoleRequest.AssumeRoleRequest()

    req.set_accept_format("json")
    req.set_RoleArn(role_arn)
    req.set_RoleSessionName("oss-python-sdk-example")

    body = clt.do_action_with_exception(req)

    j = json.loads(oss2.to_unicode(body))

    access_key_id = j["Credentials"]["AccessKeyId"]
    access_key_secret = j["Credentials"]["AccessKeySecret"]
    security_token = j["Credentials"]["SecurityToken"]

    return access_key_id, access_key_secret, security_token
Esempio n. 23
0
def upload_file(request):
    referer = request.META.get('HTTP_REFERER', reverse('home'))
    if request.method == "POST":    # 请求方法为POST时,进行处理
        myFile =request.FILES.get("myfile", None)    # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            return HttpResponse("no files for upload!")
        destination = open(os.path.join(dir_path, myFile.name), 'wb+')    # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():      # 分块写入文件
            destination.write(chunk)
        destination.close()
        with open(oss2.to_unicode(dir_path+"/"+myFile.name), 'rb') as f:
            bucket.put_object(myFile.name, f)
        os.chdir("F:\\ServerveManager\\Pycharm\\PyCharm 2018.2.2\\files\\oss_test1\\static")
        # os.chdir('../static')
        shutil.rmtree("media")
        os.mkdir("media")
        return redirect(request.GET.get('from', reverse('home')))
Esempio n. 24
0
    def get_token(self):
        # args = parameter_required(("id", ))
        from aliyunsdkcore import client
        from aliyunsdksts.request.v20150401 import AssumeRoleRequest
        import json
        # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
        # role_arn为角色的资源名称。
        from jinrui.config.secret import ACCESS_KEY_ID_STS, ACCESS_KEY_SECRET_STS, ACCESS_KEY_ROLE_ARN
        # 创建policy_text。
        # 仅允许对名称为test-bucket1的Bucket下的所有资源执行GetObject操作。
        policy_text = """
        {
            "Version": "1", 
            "Statement": [
                {
                    "Action": ["sts:AssumeRole"], 
                    "Effect": "Allow", 
                    "Resource": "*"
                },
                {
                    "Action": "oss:*",
                    "Effect": "Allow",
                    "Resource": "*"
                }
            ]
        }
        """
        clt = client.AcsClient(ACCESS_KEY_ID_STS, ACCESS_KEY_SECRET_STS, 'cn-hangzhou')
        req = AssumeRoleRequest.AssumeRoleRequest()

        # 设置返回值格式为JSON。
        req.set_accept_format('json')
        req.set_RoleArn(ACCESS_KEY_ROLE_ARN)
        req.set_RoleSessionName('session-name')
        req.set_Policy(policy_text)
        body = clt.do_action_with_exception(req)

        # 使用RAM账号的AccessKeyId和AccessKeySecret向STS申请临时token。
        token = json.loads(oss2.to_unicode(body))

        return {
            "code": 200,
            "success": True,
            "message": "获取成功",
            "data": token
        }
Esempio n. 25
0
def gen_signature(allow_prefix=None,
                  SecretId=SECRET_ID,
                  SecretKey=SECRET_KEY,
                  expire=300,
                  bucket=BUCKET,
                  method='GET',
                  session_name='nobody'):
    # endpoint = 'http://oss-%s.aliyuncs.com' % AP
    clt = client.AcsClient(SecretId, SecretKey, AP)

    policy_text = """{
            "Version": "1", 
            "Statement": [
              {"Action": ["oss:GetObject","oss:PutObject"], 
                "Effect": "Allow", 
                "Resource": ["acs:oss:*:*:%s/%s"]
              }
            ]
        }""" % (bucket, allow_prefix)
    # print(policy_text)

    req = AssumeRoleRequest.AssumeRoleRequest()
    req.set_accept_format('json')
    req.set_RoleArn(ROLE)
    req.set_RoleSessionName(session_name)
    req.set_Policy(policy_text)
    body = clt.do_action_with_exception(req)
    d = json.loads(oss2.to_unicode(body))

    # auth = oss2.StsAuth(token['Credentials']['AccessKeyId'],
    #                     token['Credentials']['AccessKeySecret'],
    #                     token['Credentials']['SecurityToken'])

    #
    # auth = oss2.Auth(SecretId, SecretKey)
    # the_bucket = oss2.Bucket(auth, endpoint, bucket)
    # surl = the_bucket.sign_url(method, allow_prefix, expire)
    d['region'] = 'oss-%s' % AP
    d['bucket'] = bucket
    return d
Esempio n. 26
0
def fetch_sts_info(access_key_id, access_key_secret, sts_role_arn):
    """子用户角色扮演获取临时用户的密钥
    :param access_key_id: 子用户的 access key id
    :param access_key_secret: 子用户的 access key secret
    :param sts_role_arn: STS角色的Arn
    :return StsInfo 返回授权用户信息对象
    """
    # 配置要访问的STS endpoint
    _REGIONID = 'cn-hongkong'
    _ENDPOINT = 'sts.cn-hongkong.aliyuncs.com'
    region_provider.add_endpoint('Sts', _REGIONID, _ENDPOINT)

    clt = client.AcsClient(access_key_id, access_key_secret, 'cn-hongkong')
    request = AssumeRoleRequest.AssumeRoleRequest()

    #request.set_accept_format('json')
    #指定角色ARN
    request.set_RoleArn(sts_role_arn)
    #设置会话名称,审计服务使用此名称区分调用者
    request.set_RoleSessionName('oss-python-sdk-example')
    #设置临时身份过期时间
    request.set_DurationSeconds(DurationSeconds)
    #发起请求,并得到response
    response = clt.do_action_with_exception(request)
    #格式化输出返回结果,将字符串结果转化为字典类型
    i = json.loads(oss2.to_unicode(response))
    #实例化StsInfo类并将临时用户信息存入对象
    global StsInfo
    StsInfo = StsInfo()
    StsInfo.access_key_id = i['Credentials']['AccessKeyId']
    StsInfo.access_key_secret = i['Credentials']['AccessKeySecret']
    StsInfo.security_token = i['Credentials']['SecurityToken']
    StsInfo.request_id = i['RequestId']
    StsInfo.expiration = oss2.utils.to_unixtime(i['Credentials']['Expiration'],
                                                '%Y-%m-%dT%H:%M:%SZ')

    #存储临时用户信息
    save_info()
Esempio n. 27
0
 def __init__(self, OSS_OPTION):  # 重写init方法
     # 获取AccessKeyId和AccessKeySecret和临时token
     accessKeyId = OSS_OPTION['AK_ID']
     accessKeySecret = OSS_OPTION['AK_SE']
     self.bucketName = OSS_OPTION['BUCKET_NAME']
     cn = OSS_OPTION['CN']
     roleArn = OSS_OPTION['roleArn']
     clt = client.AcsClient(accessKeyId, accessKeySecret, cn)
     req = AssumeRoleRequest.AssumeRoleRequest()
     policyText = '{"Version": "1", "Statement": [{"Action": ["oss:PutObject", "oss:GetObject"], ' \
                   '"Effect": "Allow", "Resource": ["acs:oss:*:*:' + self.bucketName + '/*"]}]}'
     # 设置返回值格式为JSON。
     req.set_accept_format('json')
     req.set_RoleArn(roleArn)
     req.set_RoleSessionName('session-name')
     req.set_Policy(policyText)
     body = clt.do_action_with_exception(req)
     # 使用RAM账号的AccessKeyId和AccessKeySecret向STS申请临时token。
     self.token = json.loads(oss2.to_unicode(body))
     self.AccessKeyId = self.token['Credentials']['AccessKeyId']
     self.AccessKeySecret = self.token['Credentials']['AccessKeySecret']
     self.SecurityToken = self.token['Credentials']['SecurityToken']
     self._connectOss()  # 链接oss数据库
Esempio n. 28
0
 def uploadDirToOSS(self, currentLocalFolderPath_: str, resFolderPath_: str, bucket_, remoteFolderPath_: str,
                    filters_: list):
     _filePaths = os.listdir(currentLocalFolderPath_)
     for _filePath in _filePaths:
         _path = currentLocalFolderPath_ + '/' + _filePath
         if os.path.isdir(_path):
             self.uploadDirToOSS(_path, resFolderPath_, bucket_, remoteFolderPath_, filters_)
         else:
             _haveBoo = False
             for _i in range(len(filters_)):  # 再过滤列表中查找
                 if _filePath.endswith(filters_[_i]):
                     _haveBoo = True  # 有就标示上
                     break
             if not _haveBoo:  # 不在过滤内容中,直接下一个
                 print("    " + str(_path) + " 过滤,未上传")
                 continue
             _remoteFilePath = remoteFolderPath_ + _path.split(resFolderPath_).pop()  # 上传OSS
             with open(oss2.to_unicode(_path), 'rb') as _file:
                 bucket_.put_object(_remoteFilePath, _file)
             _meta = bucket_.get_object_meta(_remoteFilePath)
             if _meta:
                 print("    " + str(_remoteFilePath) + " 上传成功 +")
             else:
                 print("    " + str(_remoteFilePath) + " 上传失败 x")

# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)


# 上传一段字符串。Object名是motto.txt,内容是一段名言。
bucket.put_object('motto.txt', 'Never give up. - Jack Ma')

# 下载到本地文件
bucket.get_object_to_file('motto.txt', '本地文件名.txt')


# 把刚刚上传的Object下载到本地文件 “座右铭.txt” 中
# 因为get_object()方法返回的是一个file-like object,所以我们可以直接用shutil.copyfileobj()做拷贝
with open(oss2.to_unicode('本地座右铭.txt'), 'wb') as f:
    shutil.copyfileobj(bucket.get_object('motto.txt'), f)


# 把本地文件 “座右铭.txt” 上传到OSS,新的Object叫做 “我的座右铭.txt”
# 注意到,这次put_object()的第二个参数是file object;而上次上传是一个字符串。
# put_object()能够识别不同的参数类型
with open(oss2.to_unicode('本地座右铭.txt'), 'rb') as f:
    bucket.put_object('云上座右铭.txt', f)


# 上面两行代码,也可以用下面的一行代码来实现
bucket.put_object_from_file('云上座右铭.txt', '本地座右铭.txt')


# 列举Bucket下10个Object,并打印它们的最后修改时间、文件名
Esempio n. 30
0
def font_identifier(image_path):
    #OCR request
    request = RecognizeCharacterRequest()
    request.set_accept_format('json')

    #Upload with SHA1 hashed name, set image to private
    file_extension = os.path.splitext(image_path)[1]
    key = hashlib.sha1(open(image_path, 'rb').read()).hexdigest() + file_extension
    bucket.put_object_from_file(key, image_path)
    bucket.put_object_acl(key, oss2.OBJECT_ACL_PRIVATE)

    #Get image info from OSS
    info = bucket.get_object(key, process = 'image/info')
    info_content = info.read()
    decoded_info = json.loads(oss2.to_unicode(info_content))

    print('Image Info ->')
    print(json.dumps(decoded_info, indent = 4, sort_keys = True))

    #Struct image URL
    image_url = bucket.sign_url('GET', key, 60)

    print('Image URL -> ' + image_url)

    #Set OCR image_url
    request.set_ImageURL(image_url)

    #Pre-config request
    min_height = int(decoded_info['ImageHeight']['value']) * float(min_height_scale)
    request.set_MinHeight(int(min_height))
    request.set_OutputProbability(True)

    #Send request to OCR server and get response
    try:
        response = client.do_action_with_exception(request)

    except Exception as error:
        print('Error -> ', error)

        #Delete OSS image
        bucket.delete_object(key)

        #Raise Exception to outsider try/except
        raise Exception(error)

    #Delete OSS image
    bucket.delete_object(key)

    #Parse json response
    parsed = json.loads(response)

    print('Response ->')
    print(json.dumps(parsed, indent = 4, sort_keys = True))

    objects = []
    distances = []
    objects_unfiltered = parsed['Data']['Results']

    #Filter probability by min_probability
    for object_unfiltered in objects_unfiltered:
        if float(object_unfiltered['Probability']) > float(min_probability):
            objects.append(object_unfiltered)

    #Cal image center O(o_x0, o_y0)
    o_x0, o_y0 = int(decoded_info['ImageWidth']['value']) / 2.0, int(decoded_info['ImageHeight']['value']) / 2.0

    for object in objects:

        #Cal TextRectangle angle A, start point A(x0, y0) and endpoint B(x1, y1)
        A = object['TextRectangles']['Angle'] / 180.0
        x0, y0 = object['TextRectangles']['Left'], object['TextRectangles']['Top']
        x1, y1 = x0 + object['TextRectangles']['Width'], y0 + object['TextRectangles']['Height']

        #Cal vector AB = (v_x0, v_y0)
        v_x0, v_y0 = x1 - x0, y1 - y0

        #Cal angle A rotated and 1/2 lenthed vector AB' = (v_x1, v_y1)
        v_x1, v_y1 = (v_x0 * math.cos(A) - v_y0 * math.sin(A)) / 2.0, (v_y0 * math.cos(A) + v_x0 * math.sin(A)) / 2.0

        #Cal TextRectangle center point B'(x2, y2)
        x2, y2 = x0 + v_x1, y0 + v_y1

        print('TextRectangleCtr -> ', (x2, y2))

        #Cal distance between point B and O
        d = math.pow(x2 - o_x0, 2) + math.pow(y2 - o_y0, 2)
        distances.append(d)

    index_min = distances.index(min(distances))

    print('Min_Index -> ', index_min)

    A = - objects[index_min]['TextRectangles']['Angle'] / 180.0

    roi = PIL.Image.open(image_path)
    roi = roi.rotate(A)

    #Cal start point A(x0, y0)
    x0, y0 = objects[index_min]['TextRectangles']['Left'], objects[index_min]['TextRectangles']['Top']

    #Cal angle A rotated A'(x1, y1)
    x1, y1 = rotate(x0, y0, o_x0, o_y0, A)

    #Crop text ROI
    roi = roi.crop((x1, y1, (x1 + objects[index_min]['TextRectangles']['Width']), (y1 + objects[index_min]['TextRectangles']['Height'])))

    #Load image and de-noisy
    tmp_img = roi.copy().convert('L')
    tmp_img = blur_image(tmp_img)
    arr_img = img_to_array(tmp_img)

    #Predict using trained model
    data = []
    data.append(arr_img)
    data = np.asarray(data, dtype = "float") / 255.0
    y = np.argmax(model.predict(data), axis = -1)

    return objects[index_min], rev_conv_label(int(y[0]))
Esempio n. 31
0
# 上传一段字符串。Object名是motto.txt,内容是一段名言。
bucket.put_object('motto.txt', 'Never give up. - Jack Ma')

# 获取Object的metadata
object_meta = bucket.get_object_meta('你的对象名')
print('last modified: ' + str(object_meta.last_modified))
print('etag: ' + object_meta.etag)
print('size: ' + str(object_meta.content_length))

# 下载到本地文件
bucket.get_object_to_file('motto.txt', '本地文件名.txt')

# 把刚刚上传的Object下载到本地文件 “座右铭.txt” 中
# 因为get_object()方法返回的是一个file-like object,所以我们可以直接用shutil.copyfileobj()做拷贝
with open(oss2.to_unicode('本地座右铭.txt'), 'wb') as f:
    shutil.copyfileobj(bucket.get_object('motto.txt'), f)

# 把本地文件 “座右铭.txt” 上传到OSS,新的Object叫做 “我的座右铭.txt”
# 注意到,这次put_object()的第二个参数是file object;而上次上传是一个字符串。
# put_object()能够识别不同的参数类型
with open(oss2.to_unicode('本地座右铭.txt'), 'rb') as f:
    bucket.put_object('云上座右铭.txt', f)

# 上面两行代码,也可以用下面的一行代码来实现
bucket.put_object_from_file('云上座右铭.txt', '本地座右铭.txt')

# 列举Bucket下10个Object,并打印它们的最后修改时间、文件名
for i, object_info in enumerate(oss2.ObjectIterator(bucket)):
    print("{0} {1}".format(object_info.last_modified, object_info.key))
Esempio n. 32
0
# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint,
                     bucket_name)

key = 'example.jpg'
new_pic = 'new-example.jpg'

# 上传示例图片
bucket.put_object_from_file(key, 'example.jpg')

# 获取图片信息
result = bucket.get_object(key, process='image/info')

json_content = result.read()
decoded_json = json.loads(oss2.to_unicode(json_content))
assert int(decoded_json['ImageHeight']['value']) == 267
assert int(decoded_json['ImageWidth']['value']) == 400
assert int(decoded_json['FileSize']['value']) == 21839
assert decoded_json['Format']['value'] == 'jpg'

# 图片缩放
# process = "image/resize,m_fixed,w_100,h_100"
# bucket.get_object_to_file(key, new_pic, process=process)
# info = get_image_info(new_pic)
# assert info[0] == 100
# assert info[1] == 100
# assert info[2] == 'JPEG'
#
# # 图片裁剪
# process = "image/crop,w_100,h_100,x_100,y_100,r_1"
    return im.height, im.width, im.format

# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

key = 'example.jpg'
new_pic = 'new-example.jpg'

# 上传示例图片
bucket.put_object_from_file(key, 'example.jpg')

# 获取图片信息
result = bucket.get_object(key, process='image/info')

json_content = result.read()
decoded_json = json.loads(oss2.to_unicode(json_content))
assert int(decoded_json['ImageHeight']['value']) == 267
assert int(decoded_json['ImageWidth']['value']) == 400
assert int(decoded_json['FileSize']['value']) == 21839
assert decoded_json['Format']['value'] == 'jpg'

# 图片缩放
process = "image/resize,m_fixed,w_100,h_100"
bucket.get_object_to_file(key, new_pic, process=process)
info = get_image_info(new_pic)
assert info[0] == 100
assert info[1] == 100
assert info[2] == 'JPEG'

# 图片裁剪
process = "image/crop,w_100,h_100,x_100,y_100,r_1"
Esempio n. 34
0
 def test_save_and_open_cn(self):
     with self.save_file(content=u'我的座右铭') as name:
         self.assertEqual(name, "test.txt")
         handle = default_storage.open(name)
         logging.info("handle: %s", handle)
         self.assertEqual(to_unicode(handle.read()), '我的座右铭')
 def corrupt_record(store, store_key, r):
     pathname = store._ResumableStoreBase__path(store_key)
     with open(oss2.to_unicode(pathname), 'w') as f:
         f.write('hello}')
Esempio n. 36
0
# 上传一段字符串。Object名是motto.txt,内容是一段名言。
# bucket.put_object('motto.txt', 'Never give up. - Jack Ma')

# 下载到本地文件
# bucket.get_object_to_file('motto.txt', '本地文件名.txt')

# 把刚刚上传的Object下载到本地文件 “座右铭.txt” 中
# 因为get_object()方法返回的是一个file-like object,所以我们可以直接用shutil.copyfileobj()做拷贝
# with open(oss2.to_unicode('本地座右铭.txt'), 'wb') as f:
# shutil.copyfileobj(bucket.get_object('motto.txt'), f)

# 把本地文件 “座右铭.txt” 上传到OSS,新的Object叫做 “我的座右铭.txt”
# 注意到,这次put_object()的第二个参数是file object;而上次上传是一个字符串。
# put_object()能够识别不同的参数类型
print('start upload [' + sys.argv[1] + '] to [' + sys.argv[2] + ']')
with open(oss2.to_unicode(sys.argv[1]), 'rb') as f:
    bucket.put_object(sys.argv[2], f)

print('completed upload [' + sys.argv[1] + '] to [' + sys.argv[2] + ']')

# 上面两行代码,也可以用下面的一行代码来实现
# bucket.put_object_from_file('云上座右铭.txt', '本地座右铭.txt')

# 列举Bucket下10个Object,并打印它们的最后修改时间、文件名
# for i, object_info in enumerate(oss2.ObjectIterator(bucket)):
# print("{0} {1}".format(object_info.last_modified, object_info.key))
#
# if i >= 9:
# break

# 删除名为motto.txt的Object
 def corrupt_record(store, store_key, r):
     pathname = store._ResumableStoreBase__path(store_key)
     with open(oss2.to_unicode(pathname), 'w') as f:
         f.write('hello}')