Beispiel #1
0
    def upload_data(self, user_id, data, file_name):
        now = int(time.time())
        if not hasattr(
                self, "token"
        ) or self.token is None or self.token.expiration < now - 1:
            self.token = self.fetch_sts_token()
        auth = oss2.StsAuth(self.token.access_key_id,
                            self.token.access_key_secret,
                            self.token.security_token)
        bucket = oss2.Bucket(auth, AliyunOss.endpoint, AliyunOss.bucket_name)
        # bucket.put_object('test2/motto.txt', 'Never give up. - Jack Ma')
        file_key = "%s/%s" % (user_id, file_name)
        bucket.put_object(file_key, data)
        return self.backet_host + "/" + file_key


# token = AliyunOss.fetch_sts_token()

# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
# token = AliyunOss.fetch_sts_token()
# auth = oss2.StsAuth(token.access_key_id, token.access_key_secret, token.security_token)
# bucket = oss2.Bucket(auth, AliyunOss.endpoint, AliyunOss.bucket_name)
#
#
# # 上传一段字符串。Object名是motto.txt,内容是一段名言。
# bucket.put_object('test2/motto.txt', 'Never give up. - Jack Ma')
Beispiel #2
0
    def upload_file(self, filename, data):
        try:
            response = requests.get("https://jinrui.sanbinit.cn/api/file/get_token")
            content = json.loads(response.content)
            token = content.get('data')
        except Exception as e:
            self.log.error('获取auth 失败 {}'.format(e))
            print('获取auth 失败 {}'.format(e))
            return False

        if not token:
            self.log.error('获取auth 失败 token 为空')
            print('获取auth 失败 token 为空')
            return False
        try:
            auth = oss2.StsAuth(token['Credentials']['AccessKeyId'],
                                token['Credentials']['AccessKeySecret'],
                                token['Credentials']['SecurityToken'])
            bucket = oss2.Bucket(auth, endpoint, bucket_name)
            bucket.put_object_from_file(filename, data)
        except Exception as e:
            self.log.error('{} 文件上传到oss 失败 {}'.format(data, e))
            print('{} 文件上传到oss 失败 {}'.format(data, e))
            return False

        return True
Beispiel #3
0
def handler(event, context):
  logger = logging.getLogger()
  evt = json.loads(event)
  logger.info("Handling event: %s", evt)
  creds = context.credentials
  endpoint = os.environ['OSS_ENDPOINT']
  if creds.security_token != None:
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
  else:
    # for local testing, use the public endpoint
    endpoint = str.replace(endpoint, "-internal", "")
    auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)

  bucket = oss2.Bucket(auth, endpoint, evt["bucket"])
  result = bucket.list_objects(prefix = evt["prefix"], marker =  evt["marker"], delimiter = evt["delimiter"], max_keys = 100)
  keys = []
  for obj in result.object_list:
    keys.append(obj.key)
  logger.info("Found %d objects", len(keys))

  return {
    "keys": keys,
    "hasMore": result.is_truncated,
    "marker": result.next_marker
  }
Beispiel #4
0
def handler(event, context):
    """
    Replicate the object from OSS to s3.
    event:   The OSS event json string. Including oss object uri and other information.
    context: The function context, including credential and runtime info.
             For detail info, please refer to https://help.aliyun.com/document_detail/56316.html#using-context
    """
    evt_list = json.loads(event)
    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                        creds.security_token)

    # Parse the event to get the source object info.
    evt = evt_list['events'][0]
    bucket_name = evt['oss']['bucket']['name']
    endpoint = 'oss-' + evt['region'] + '.aliyuncs.com'
    bucket = oss2.Bucket(auth, endpoint, bucket_name)
    object_name = evt['oss']['object']['key']
    print(object_name)
    # Download the oss object.
    remote_stream = bucket.get_object(object_name)
    if not remote_stream:
        raise RuntimeError('failed to get oss object. bucket: %s. object: %s' %
                           (bucket_name, object_name))
    print 'download object from oss success: %s' % object_name
    content = remote_stream.read()
    # replicate to AWS S3
    S3_bucket_name = 'changeme'
    client = boto3.client('s3',
                          aws_access_key_id='AK(changeme)',
                          aws_secret_access_key='SK(changeme)',
                          region_name='cn-north-1')
    #client.upload_file(object_name,S3_bucket_name,object_name)
    client.put_object(Bucket=S3_bucket_name, Key=object_name, Body=content)
Beispiel #5
0
def handler(event, context):
    package_dir = os.environ['PackageDir']
    filename = os.environ['FileName']
    source = '/tmp/{}'.format(filename)
    dest = filename

    cmd = 'tar -cpzf {} --numeric-owner --ignore-failed-read {}'.format(
        source, package_dir)

    subprocess.call(['sh', '-c', cmd])

    print('Zipping done! Uploading...')

    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                        creds.security_token)

    endpoint = os.environ['OSSEndpoing']
    bucket = os.environ['Bucket']

    print('endpoint: ' + endpoint)
    print('bucket: ' + bucket)

    bucket = oss2.Bucket(auth, endpoint, bucket)

    bucket.put_object_from_file(dest, source)

    return 'Zipping done and uploading done!'
Beispiel #6
0
def get_file_url(server_name, local_file):
    sts_token = request_sts_token('kevin')

    auth = oss2.StsAuth(sts_token.access_key_id, sts_token.access_key_secret, sts_token.security_token)
    bucket = oss2.Bucket(auth, 'https://oss-cn-beijing.aliyuncs.com', 'apl-docs')
    url = bucket.sign_url('GET', server_name, 1800, params={"response-content-disposition": "attachment; filename=%s" % local_file})
    return url
Beispiel #7
0
def handler(event, context):

    logger.info("event: " + event)
    evt = json.loads(event)
    url = evt['url']

    html = get_html(url)

    img_list = get_img(html)
    logger.info(img_list)

    creds = context.credentials

    if (local):
        auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)
    else:
        auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                            creds.security_token)

    bucket = oss2.Bucket(auth, endpoint, bucket_name)

    count = 0
    for item in img_list:
        count += 1
        logging.info(item)
        # Get each picture
        pic = urllib.urlopen(item)
        # Store all the pictures in oss bucket, keyed by timestamp in microsecond unit
        bucket.put_object(
            str(datetime.datetime.now().microsecond) + '.png', pic)

    return 'Done!'
Beispiel #8
0
def uoload_OSS_Server():

    auth = oss2.StsAuth(
        accessKeyID, accessKeySecret, securityToken)
    oss_bucket = oss2.Bucket(auth, endpoint, bucket)

    callback_param = {}
    callback_param['version'] = version
    callback_param['platform'] = '1'
    callback_param['event_userid'] = userid
    callback_param['event_openid'] = openid
    callback_param['unionid'] = unionid
    callback_param['type'] = filetype
    callback_param['swingid'] = swingid
    callback_param['videoNumber'] = number
    callback_param['sessionid'] = sessionid
    callback_param['bucket'] = bucket
    callback_param['endpoint'] = endpoint
    callback_param['objectKey'] = serverpath + filename

    callback_dict = {}
    callback_dict['callbackUrl'] = Host + "/pc/oss/callback"
    callback_dict['callbackBody'] = join_DictTo_HTML_Param(callback_param)
    # print joinDictToHTMLParam(callback_param)

    callback_str = json.dumps(callback_dict).strip()
    base64_callback_body = base64.b64encode(callback_str)
    headers = {'x-oss-callback': base64_callback_body}

    print serverpath

    state = oss_bucket.put_object_from_file(serverpath+filename, FilePath+filename, headers)
    print state
def handler(event, context):
    logger.info('Receive event: {}'.format(event))

    # Read params
    data = json.loads(event)
    endpoint = data['endpoint']
    bucket_name = data['bucketName']
    marker = data.get('marker', '')
    prefix = data.get('prefix', '')
    max_keys = data.get('maxKeys', 100)

    # Sts
    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                        creds.security_token)
    bucket = oss2.Bucket(auth, endpoint, bucket_name)

    # list achieve files
    achieve_list = []
    resp = bucket.list_objects(prefix=prefix, marker=marker, max_keys=max_keys)
    for obj in resp.object_list:
        if obj.storage_class == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
            achieve_list.append(obj.key)

    # Capsule all data into json and return
    res = {
        'bucketName': bucket_name,  # bucket name
        'files': achieve_list,  # achieve file list
        'marker': resp.next_marker,  # next maker for list object
        'end': not resp.is_truncated,  # whether all objects been listed
    }
    return json.dumps(res)
  def uploadData(self, localPath, uploadName):
    print 'uploading data'
    req = request.Request(host=self.__host, protocol=constant.HTTP, url='/credential/upload/data/' + uploadName, method="GET",
                          time_out=3000)
    a, b, rr = self.cli.execute(req)

    try:
      res = json.loads(rr)
    except:
      print a, b, rr
      return

    if res['success'] != True:
      print res
      return

    token = res['credential']

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

    bucket = oss2.Bucket(auth, self.__endpoint, self.__bucket)

    oss2.resumable_upload(bucket, res['path'], localPath)

    print 'upload complete'
Beispiel #11
0
    def get_oss_url(self, file_path, suffix, is_local):

        request = GetOssStsTokenRequest.GetOssStsTokenRequest()
        request.set_endpoint("viapiutils.cn-shanghai.aliyuncs.com")

        response = self.client.do_action_with_exception(request)
        response_data = json.loads(response, encoding="UTF-8").get('Data')
        token = response_data.get('SecurityToken')
        ak_key = response_data.get('AccessKeyId')
        ak_sec = response_data.get('AccessKeySecret')

        auth = oss2.StsAuth(ak_key, ak_sec, token)

        bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com',
                             'viapi-customer-temp')
        oss_key = self.access_key + "/" + str(uuid.uuid4()) + "." + suffix
        oss_url = 'http://viapi-customer-temp.oss-cn-shanghai.aliyuncs.com/' + oss_key
        if is_local:
            with open(file_path, "rb") as image:

                bucket.put_object(oss_key, image)
                return oss_url
        else:
            with url_request.urlopen(file_path,
                                     cafile=certifi.where()) as image:
                bucket.put_object(oss_key, image.read())
                return oss_url
Beispiel #12
0
 def upload_bankcard(self, bankname, bankCardFile):
     """上传银行卡公共方法"""
     #获取AccessKeyId,AccessKeySecret,SecurityToken
     response = self.login.create_api(ALI_GetAliSTS)
     self.AccessKeyId = response.json()['Data']['AccessKeyId']
     self.AccessKeySecret = response.json()['Data']['AccessKeySecret']
     self.SecurityToken = response.json()['Data']['SecurityToken']
     #获取auth
     auth = oss2.StsAuth(self.AccessKeyId, self.AccessKeySecret,
                         self.SecurityToken)
     #获取bucket对象
     bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com',
                          'woda-app-private-test')
     # 获取BankCardUrl的参数和上传到OSS的文件名
     fname = os.path.basename(bankCardFile)
     BankCardpath = f'zxx/BankCard/tmp_{fname}'
     #上传文件
     result = bucket.put_object_from_file(BankCardpath, bankCardFile)
     pprint.pprint(result)
     #调用上传银行卡接口
     res = self.login.create_api(AddBankCard_Api,
                                 ProvinceId=-9999,
                                 ProvinceName='请选择',
                                 CityId=-9999,
                                 CityName='请选择',
                                 AreaName='请选择',
                                 CfgBankId=None,
                                 BankName=bankname,
                                 BankCardUrl=bankCardFile)
     pprint.pprint(res.json())
     return res.json()
Beispiel #13
0
    def upload_workcard(self, entname, workCardFile):
        """上传工牌公共方法"""
        #获取企业列表
        self.get_ent(entname)
        entid = self.entid
        #获取AccessKeyId,AccessKeySecret,SecurityToken
        response = self.login.create_api(ALI_GetAliSTS)
        self.AccessKeyId = response.json()['Data']['AccessKeyId']
        self.AccessKeySecret = response.json()['Data']['AccessKeySecret']
        self.SecurityToken = response.json()['Data']['SecurityToken']
        #获取auth
        auth = oss2.StsAuth(self.AccessKeyId, self.AccessKeySecret,
                            self.SecurityToken)
        #获取bucket对象
        bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com',
                             'woda-app-public-test')
        # 获取IdCardFrontUrl的参数和上传到OSS的文件名
        fname = os.path.basename(workCardFile)
        WorkCardpath = f'zhjz/WorkCard/tmp_{fname}'
        #上传文件
        result = bucket.put_object_from_file(WorkCardpath, workCardFile)

        #调用上传工牌接口
        res = self.login.create_api(UploadWorkCard_Api,
                                    EntId=entid,
                                    WorkCardUrl=WorkCardpath)
        pprint.pprint(res.json())
        return res.json()
 def __init__(self,ak,secret,token,endpoint):
     arg_dict=locals()
     globalEnv.logger.debug('code:{}.{} {}'.format(__name__,sys._getframe().f_code.co_name ,str(arg_dict)))
     self.__auth=oss2.StsAuth(ak,secret,token,auth_version=oss2.AUTH_VERSION_2)
     self.__endpoint=endpoint
     globalEnv.logger.info('oss init complete')
     return 
Beispiel #15
0
def handler(event, context):
    evt = json.loads(event)
    oss_html_path = "process/sina_html/"

    region = "cn-hangzhou-internal"
    bucketname = "leo-hangzhou"
    endpoint = 'oss-{0}.aliyuncs.com'.format(region)
    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
    bucket = oss2.Bucket(auth, endpoint, bucketname)
    logger = logging.getLogger()
    logger.info(
        (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime(
            '%Y-%m-%d %H:%M:%S') + ' Step 1: Starting to parse url')
    try:
        json_key = evt['events'][0]['oss']['object']['key']
        print(json_key)
        print(bucket.get_object(json_key).read())
        news_url = bucket.get_object(json_key).read().decode('utf-8')

        print(news_url)
        logger.info(
            (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime(
                '%Y-%m-%d %H:%M:%S') + ' Step 2: Start to download ' + news_url)
        news_r = requests.get(news_url, timeout=10)  # 请求超时时间为10秒
        news_r.raise_for_status()  # 如果状态不是200,则引发异常
        news_r.encoding = news_r.apparent_encoding  # 配置编码
        html_file = oss_html_path + news_url.replace('http://', '')
        bucket.put_object(html_file, '{0}'.format(news_r.text))

        print(news_url)
    except Exception as e:
        print("产生异常" + str(e))
Beispiel #16
0
def handler(event, context):
    LOGGER.info(event)
    evt = json.loads(event)
    oss_bucket_name = evt["bucket_name"]
    object_key = evt["object_key"]
    output_dir = evt["output_dir"]
    tile = evt["tile"]
    ss = evt.get("start", 0)
    ss = str(ss)
    t = evt.get("duration")
    if t:
        t = str(t)

    itsoffset = evt.get("itsoffset", 0)
    itsoffset = str(itsoffset)
    scale = evt.get("scale", "-1:-1")
    interval = str(evt.get("interval", 1))
    padding = str(evt.get("padding", 0))
    color = str(evt.get("color", "black"))
    dst_type = str(evt.get("dst_type", "jpg"))

    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId,
                        creds.accessKeySecret, creds.securityToken)
    oss_client = oss2.Bucket(
        auth, 'oss-%s-internal.aliyuncs.com' % context.region, oss_bucket_name)

    input_path = oss_client.sign_url('GET', object_key, 3600)
    fileDir, shortname, extension = get_fileNameExt(object_key)

    cmd = ['/code/ffmpeg', '-ss', ss, '-itsoffset', itsoffset, '-y', '-i', input_path,
           '-f', 'image2', '-vf', "fps=1/{0},scale={1},tile={2}:padding={3}:color={4}".format(
               interval, scale, tile, padding, color),
           '/tmp/{0}%d.{1}'.format(shortname, dst_type)]

    if t:
        cmd = ['/code/ffmpeg', '-ss', ss, '-itsoffset', itsoffset, '-t', t, '-y', '-i', input_path,
               '-f', 'image2', '-vf', "fps=1/{0},scale={1},tile={2}:padding={3}:color={4}".format(
                   interval, scale, tile, padding, color),
               '/tmp/{0}%d.{1}'.format(shortname, dst_type)]

    LOGGER.info("cmd = {}".format(" ".join(cmd)))
    try:
        subprocess.run(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
    except subprocess.CalledProcessError as exc:
        LOGGER.error('returncode:{}'.format(exc.returncode))
        LOGGER.error('cmd:{}'.format(exc.cmd))
        LOGGER.error('output:{}'.format(exc.output))
        LOGGER.error('stderr:{}'.format(exc.stderr))
        LOGGER.error('stdout:{}'.format(exc.stdout))

    for filename in os.listdir('/tmp/'):
        filepath = '/tmp/' + filename
        if filename.startswith(shortname):
            filekey = os.path.join(output_dir, fileDir, filename)
            oss_client.put_object_from_file(filekey, filepath)
            os.remove(filepath)
            LOGGER.info("Uploaded {} to {}".format(filepath, filekey))
    return "ok"
Beispiel #17
0
def download_and_unzip(objectKey, path, context):
    creds = context.credentials
    if (local):
        print('thank you for running function in local!!!!!')
        auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)

    else:
        auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                            creds.security_token)

    endpoint = os.environ['Endpoint']
    bucket = os.environ['Bucket']

    print('objectKey: ' + objectKey)
    print('path: ' + path)
    print('endpoint: ' + endpoint)
    print('bucket: ' + bucket)

    bucket = oss2.Bucket(auth, endpoint, bucket)

    zipName = '/tmp/tmp.zip'

    print('before downloading' + objectKey + '...')
    start_download_time = time.time()
    bucket.get_object_to_file(objectKey, zipName)
    print('before unzipping ' + objectKey + ' ...')
    start_unzip_time = time.time()
    with zipfile.ZipFile(zipName, "r") as z:
        z.extractall(path)
    print('unzipping done, used %s seconds...' %
          (time.time() - start_unzip_time))
Beispiel #18
0
def get_oss_client(evt, context):
    region = evt['region']
    bucket = evt['bucket']
    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
    oss_client = oss2.Bucket(auth, 'oss-' + region + '-internal.aliyuncs.com', bucket)
    return oss_client
Beispiel #19
0
 def uploadoss(self,generateSign,userId):#localfile,remotePath,
     '''
     本地图片上传阿里云
     userId
     generateSign
     '''
     accessKeySecret = generateSign["data"]["accessKeySecret"]
     securityToken = generateSign["data"]["securityToken"]
     accesskeyId = generateSign["data"]["accessKeyId"]
     # millis = generateSign["data"]["expiration"]
     bucket_name = 'youtime-test'
     endpoint = 'oss-cn-beijing.aliyuncs.com'
     # 创建存储空间实例,所有文件相关的方法都需要通过存储空间实例来调用。
     # ossAuth = oss2.Auth(accesskeyId, accessKeySecret)
     ossAuth = oss2.StsAuth(accesskeyId, accessKeySecret,securityToken)
     bucket = oss2.Bucket(ossAuth, endpoint, bucket_name) #enable_crc=False
     # 上传图片
     pictures,imgdir = self.get_picture()
     imgArr = random.sample(pictures,3)
     print("imgArr===={}".format(imgArr))
     imgArr2 = []
     millis = int(round(time.time() * 1000))
     for i, img_path in enumerate(imgArr):
         key = "{}{}{}.png".format(str(millis),str(userId),str(i+1))
         bucket.put_object_from_file(key, imgdir+"/"+img_path)
         # print("result===={}==={}".format(key,result.status))
         # 生成带签名的URL,并指定过期时间为10分钟。过期时间单位是秒。
         style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
         url = bucket.sign_url('GET', key, 10 * 60, params={'x-oss-process': style})
         # ret = bucket.sign_url('GET', key, 60*60*24)  # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s)
         imgArr2.append(url.split("?")[0])
     print("价值图片===={}".format(imgArr2))
     return imgArr2,millis
Beispiel #20
0
def download_and_unzip_if_not_exist(bucket, objectKey, path, context):
    creds = context.credentials

    if local:
        print('thank you for running function in local!!!!!!')
        auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)
    else:
        auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                            creds.security_token)

    print('objectKey: ' + objectKey)
    print('path: ' + path)
    print('endpoint: ' + endpoint)
    print('bucket: ' + bucket)

    bucketObj = oss2.Bucket(auth, endpoint, bucket)

    zipName = '/tmp/tmp.zip'

    print('before downloading ' + objectKey + ' ...')
    start_download_time = time.time()
    bucketObj.get_object_to_file(objectKey, zipName)
    print('after downloading, used %s seconds...' %
          (time.time() - start_download_time))

    if not os.path.exists(path):
        os.mkdir(path)

    print('before unzipping ' + objectKey + ' ...')
    start_unzip_time = time.time()
    with zipfile.ZipFile(zipName, "r") as z:
        z.extractall(path)
    print('unzipping done, used %s seconds...' %
          (time.time() - start_unzip_time))
def upload_to_oss(fpath, prefix='ATTA_', fobjname=None):
    if not fobjname:
        # fobjname = os.path.split(fpath)[1]
        extname = os.path.splitext(fpath)[-1]
        fobjname = prefix + str(uuid.uuid4()) + extname
    oss_info = get_oss_info()

    # 'http://oss-cn-hangzhou.aliyuncs.com'
    oss_region_url = 'http://' + oss_info['region'] + '.aliyuncs.com'
    # auth = oss2.Auth(oss_info['accessKeyId'], oss_info['accessKeySecret'])
    auth = oss2.StsAuth(oss_info['accessKeyId'], oss_info['accessKeySecret'],
                        oss_info['stsToken'])
    bucket = oss2.Bucket(auth, oss_region_url, oss_info['bucket'])

    # # 必须以二进制的方式打开文件,因为需要知道文件包含的字节数。
    # with open('<yourLocalFile>', 'rb') as fileobj:
    #     # Seek方法用于指定从第1000个字节位置开始读写。上传时会从您指定的第1000个字节位置开始上传,直到文件结束。
    #     fileobj.seek(1000, os.SEEK_SET)
    #     # Tell方法用于返回当前位置。
    #     current = fileobj.tell()
    #     bucket.put_object('<yourObjectName>', fileobj)

    response_obj = bucket.put_object_from_file(fobjname, fpath)
    expire = 60 * 60 * 24 * 365 * 100  # 100 year
    signed = bucket.sign_url('GET', fobjname, expire, slash_safe=True)
    return signed.split('?')[0]
Beispiel #22
0
def handler(event, context):
    logger.info('Receive event: {}'.format(event))

    # Read params
    data = json.loads(event)
    endpoint = data['endpoint']
    bucket_name = data['bucketName']
    files = data['files']

    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
    bucket = oss2.Bucket(auth, endpoint, bucket_name)

    # Check file restore completed
    status = 'success'
    for file_name in files:
        resp = bucket.head_object(file_name)
        logger.info('Head object response headers: {}'.format(resp.headers))
        restore_header = 'x-oss-restore'
        if restore_header not in resp.headers:
            # restore request not submit or out of expire date
            status = 'success'
        elif resp.headers[restore_header].find('ongoing-request="false"') != -1:
            status = 'success'
        else:
            status = 'running'
            break

    return '{"status": "%s"}' % status
    def get_oss_url(self, file_path, suffix, is_local):

        request = GetOssStsTokenRequest.GetOssStsTokenRequest()
        request.set_endpoint("viapiutils.cn-shanghai.aliyuncs.com")

        response = self.client.do_action_with_exception(request)
        #response_data = json.loads(response,encoding="UTF-8").get('Data')
        response_data = json.loads(
            response.decode(encoding='UTF-8')).get('Data')
        token = response_data.get('SecurityToken')
        ak_key = response_data.get('AccessKeyId')
        ak_sec = response_data.get('AccessKeySecret')

        auth = oss2.StsAuth(ak_key, ak_sec, token)

        bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com',
                             'viapi-customer-temp')
        oss_key = self.access_key + "/" + str(uuid.uuid4()) + "." + suffix
        oss_url = 'http://viapi-customer-temp.oss-cn-shanghai.aliyuncs.com/' + oss_key
        if is_local:
            with open(file_path, "rb") as image:

                bucket.put_object(oss_key, image)
                return oss_url
        else:
            headers = {
                'User-Agent':
                'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'
            }
            req = url_request.Request(file_path, headers=headers)
            #with url_request.urlopen(file_path, cafile=certifi.where()) as image:
            with url_request.urlopen(req, cafile=certifi.where()) as image:
                bucket.put_object(oss_key, image.read())
                return oss_url
Beispiel #24
0
def handler(event, context):
  logger = logging.getLogger()
  evt = json.loads(event)
  logger.info("Handling event: %s", evt)
  creds = context.credentials
  endpoint = os.environ['OSS_ENDPOINT']
  if creds.security_token != None:
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
  else:
    # for local testing, use the public endpoint
    endpoint = str.replace(endpoint, "-internal", "")
    auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)

  bucket = oss2.Bucket(auth, endpoint, evt["bucket"])

  tmpdir = '/tmp/download/'
  imagePath = tmpdir + "old"
  newImagePath = tmpdir + "new.jpg"
  
  os.system("rm -rf /tmp/*")
  os.mkdir(tmpdir)
  
  #download
  bucket.get_object_to_file(evt["key"] , imagePath)

  cascPath = "/code/haarcascade_frontalface_default.xml"

  # Create the haar cascade
  faceCascade = cv2.CascadeClassifier(cascPath)

  # Read the image
  image = cv2.imread(imagePath)
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

  # Detect faces in the image
  faces = faceCascade.detectMultiScale(
      gray,
      scaleFactor=1.1,
      minNeighbors=3,
      minSize=(30, 30),
      flags=cv2.CASCADE_SCALE_IMAGE
  )

  logger.info("Found %d faces!", len(faces))

  # Draw a rectangle around the faces
  for (x, y, w, h) in faces:
      cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

  # write the image to a file
  cv2.imwrite(newImagePath, image)
  
  # upload
  bucket.put_object_from_file("face-detection/" + evt["key"], newImagePath)

  # move and clean
  bucket.put_object_from_file("processed/" + evt["key"], imagePath)
  bucket.delete_object(evt["key"])

  return {"faces": len(faces)}
Beispiel #25
0
def handler(event, context):
    LOGGER.info(event)
    evt = json.loads(event)
    oss_bucket_name = evt["bucket_name"]
    object_key = evt["object_key"]
    output_dir = evt["output_dir"]
    dst_type = evt["dst_type"]
    ac = evt.get("ac")
    ar = evt.get("ar")

    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret,
                        creds.securityToken)
    oss_client = oss2.Bucket(auth,
                             'oss-%s-internal.aliyuncs.com' % context.region,
                             oss_bucket_name)

    input_path = oss_client.sign_url('GET', object_key, 15 * 60)
    fileDir, shortname, extension = get_fileNameExt(object_key)

    cmd = [
        '/code/ffmpeg', '-i', input_path,
        '/tmp/{0}{1}'.format(shortname, dst_type)
    ]
    if ac:
        if ar:
            cmd = [
                '/code/ffmpeg', '-i', input_path, "-ac",
                str(ac), "-ar",
                str(ar), '/tmp/{0}{1}'.format(shortname, dst_type)
            ]
        else:
            cmd = [
                '/code/ffmpeg', '-i', input_path, "-ac",
                str(ac), '/tmp/{0}{1}'.format(shortname, dst_type)
            ]
    else:
        if ar:
            cmd = [
                '/code/ffmpeg', '-i', input_path, "-ar",
                str(ar), '/tmp/{0}{1}'.format(shortname, dst_type)
            ]

    LOGGER.info("cmd = {}".format(" ".join(cmd)))
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError as exc:
        LOGGER.error('returncode:{}'.format(exc.returncode))
        LOGGER.error('cmd:{}'.format(exc.cmd))
        LOGGER.error('output:{}'.format(exc.output))

    for filename in os.listdir('/tmp/'):
        filepath = '/tmp/' + filename
        if filename.startswith(shortname):
            filekey = os.path.join(output_dir, fileDir, filename)
            oss_client.put_object_from_file(filekey, filepath)
            os.remove(filepath)
            LOGGER.info("Uploaded {} to {}".format(filepath, filekey))
    return "ok"
Beispiel #26
0
def handler(event, context):
    LOGGER.info(event)
    evt = json.loads(event)
    oss_bucket_name = evt["bucket_name"]
    object_key = evt["object_key"]
    output_dir = evt["output_dir"]
    vframes = evt.get("vframes")
    if vframes:
        vframes = str(vframes)
    ss = evt.get("start", 0)
    ss = str(ss)
    t = evt.get("duration")
    if t:
        t = str(t)
    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret,
                        creds.securityToken)
    oss_client = oss2.Bucket(auth,
                             'oss-%s-internal.aliyuncs.com' % context.region,
                             oss_bucket_name)

    input_path = oss_client.sign_url('GET', object_key, 15 * 60)
    fileDir, shortname, extension = get_fileNameExt(object_key)
    gif_path = os.path.join("/tmp", shortname + ".gif")

    cmd = [
        "/code/ffmpeg", "-y", "-ss", ss, "-accurate_seek", "-i", input_path,
        "-pix_fmt", "rgb24", gif_path
    ]
    if t:
        cmd = [
            "/code/ffmpeg", "-y", "-ss", ss, "-t", t, "-accurate_seek", "-i",
            input_path, "-pix_fmt", "rgb24", gif_path
        ]
    else:
        if vframes:
            cmd = [
                "/code/ffmpeg", "-y", "-ss", ss, "-accurate_seek", "-i",
                input_path, "-vframes", vframes, "-y", "-f", "gif", gif_path
            ]

    LOGGER.info("cmd = {}".format(" ".join(cmd)))
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError as exc:
        LOGGER.error('returncode:{}'.format(exc.returncode))
        LOGGER.error('cmd:{}'.format(exc.cmd))
        LOGGER.error('output:{}'.format(exc.output))

    gif_key = os.path.join(output_dir, fileDir, shortname + ".gif")

    oss_client.put_object_from_file(gif_key, gif_path)

    LOGGER.info("Uploaded {} to {} ".format(gif_path, gif_key))

    os.remove(gif_path)

    return "ok"
def handler(event, context):
    """
  The object from OSS will be decompressed automatically .
  param: event:   The OSS event json string. Including oss object uri and other information.
      For detail info, please refer https://help.aliyun.com/document_detail/70140.html?spm=a2c4g.11186623.6.578.5eb8cc74AJCA9p#OSS
  
  param: context: The function context, including credential and runtime info.

      For detail info, please refer to https://help.aliyun.com/document_detail/56316.html#using-context
  """
    evt_lst = json.loads(event)
    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret,
                        creds.security_token)

    evt = evt_lst['events'][0]
    bucket_name = evt['oss']['bucket']['name']
    endpoint = 'oss-' + evt['region'] + '-internal.aliyuncs.com'
    bucket = oss2.Bucket(auth, endpoint, bucket_name)
    object_name = evt['oss']['object']['key']

    file_type = os.path.splitext(object_name)[1]

    if file_type != ".zip":
        raise RuntimeError('{} filetype is not zip'.format(object_name))

    LOGGER.info("start to decompress zip file = {}".format(object_name))

    lst = object_name.split("/")
    zip_name = lst[-1]
    PROCESSED_DIR = os.environ.get("PROCESSED_DIR", "")
    if PROCESSED_DIR and PROCESSED_DIR[-1] != "/":
        PROCESSED_DIR += "/"
    newKey = PROCESSED_DIR + zip_name
    zip_fp = helper.OssStreamFileLikeObject(bucket, object_name)

    newKey = newKey.replace(".zip", "/")

    with helper.zipfile_support_oss.ZipFile(zip_fp) as zip_file:
        for name in zip_file.namelist():
            with zip_file.open(name) as file_obj:
                try:
                    name = name.encode(encoding='cp437')
                except:
                    name = name.encode(encoding='utf-8')

                # the string to be detect is long enough, the detection result accuracy is higher
                detect = chardet.detect((name * 100)[0:100])
                confidence = detect["confidence"]
                if confidence > 0.8:
                    try:
                        name = name.decode(encoding=detect["encoding"])
                    except:
                        name = name.decode(encoding='gb2312')
                else:
                    name = name.decode(encoding="gb2312")

                bucket.put_object(newKey + name, file_obj)
Beispiel #28
0
def oss_getfile(context, region, bucketname, objectname, localfile):
    endpoint = 'oss-{0}.aliyuncs.com'.format(region)
    creds = context.credentials
    auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret,
                        creds.securityToken)
    bucket = oss2.Bucket(auth, endpoint, bucketname)

    if bucket.object_exists(objectname):
        bucket.get_object_to_file(objectname, localfile)
Beispiel #29
0
def get_oss_client(context, endpoint, bucket):
  creds = context.credentials
  if creds.security_token != None:
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
  else:
    # for local testing, use the public endpoint
    endpoint = str.replace(endpoint, "-internal", "")
    auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)
  return oss2.Bucket(auth, endpoint, bucket)
Beispiel #30
0
 def set_aliyun(self, account_info):
     # print "set_aliyun", account_info
     self.account_info = account_info
     self.sts_auth = oss2.StsAuth(self.account_info.access_key_id,
                                  self.account_info.access_key_secret,
                                  self.account_info.security_token)
     self.oss_bucket = self.account_info.oss_bucket
     self.oss_endpoint = self.account_info.oss_endpoint
     self.account_local_time = time.time()