def _do_package_core(self, func_path, namespace, func_name, region=None): zipfile, zip_file_name, zip_file_name_cos = self._zip_func(func_path, namespace, func_name) code_url = dict() file_size = os.path.getsize(os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name)) Operation("Package name: %s, package size: %s kb" % (zip_file_name, str(file_size / 1000))).process() default_bucket_name = "" if UserConfig().using_cos.upper().startswith("TRUE"): using_cos = True default_bucket_name = "scf-deploy-" + region + "-" + str(UserConfig().appid) else: using_cos = False if self.without_cos: size_infor = self.file_size_infor(file_size) # size_infor = -1 if size_infor == -1: msg = 'Your package %s is too large and needs to be uploaded via COS.' % (zip_file_name) Operation(msg).warning() msg = 'You can use --cos-bucket BucketName to specify the bucket, or you can use the "scf configure set" to set the default to open the cos upload.' Operation(msg).warning() return elif size_infor == 0: Operation("Package %s size is over 8M, it is highly recommended that you upload using COS. " % ( zip_file_name)).information() Operation("Uploading this package without COS.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("%s Upload success" % (zip_file_name)).success() elif self.cos_bucket: bucket_name = self.cos_bucket + "-" + UserConfig().appid Operation("Uploading this package to COS, bucket_name: %s" % (bucket_name)).process() CosClient(region).upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name_cos) Operation("Upload success").success() code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() elif using_cos: Operation("By default, this package will be uploaded to COS.").information() Operation("Default COS-bucket: " + default_bucket_name).information() Operation( "If you don't want to upload the package to COS by default, you could change your configure!").information() cos_client = CosClient(region) Operation("Checking you COS Bucket: %s." % (default_bucket_name)).process() cos_bucket_status = cos_client.get_bucket(default_bucket_name) if cos_bucket_status == 0: # 未获得到bucket Operation("Creating default COS Bucket: " + default_bucket_name).process() create_status = cos_client.create_bucket(bucket=default_bucket_name) if create_status == True: cos_bucket_status = 1 Operation("Creating success. Cos Bucket name: " + default_bucket_name).success() else: Operation("Creating Cos Bucket: %s faild." % (default_bucket_name)).warning() cos_bucket_status = create_status try: if "<?xml" in str(create_status): error_code = re.findall("<Code>(.*?)</Code>", str(create_status))[0] error_message = re.findall("<Message>(.*?)</Message>", str(create_status))[0] Operation("COS client error code: %s, message: %s" % (error_code, error_message)).warning() except: pass if cos_bucket_status == 1: try: # 获取bucket正常,继续流程 file_data = zipfile.read() md5 = hashlib.md5(file_data).hexdigest() is_have = 0 try: object_list = cos_client.get_object_list( bucket=default_bucket_name, prefix=str(namespace) + "-" + str(func_name) ) if isinstance(object_list, dict) and 'Contents' in object_list: for eve_object in object_list["Contents"]: if md5 in eve_object["ETag"]: response = cos_client.copy_object( default_bucket_name, eve_object["Key"], zip_file_name_cos, ) is_have = 1 break except: pass if is_have == 0: Operation("Uploading to COS, bucket name: " + default_bucket_name).process() # 普通上传 cos_client.upload_file2cos( bucket=default_bucket_name, file=file_data, key=zip_file_name_cos ) # 分块上传 # cos_client.upload_file2cos2( # bucket=default_bucket_name, # file=os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name), # key=zip_file_name_cos, # md5=md5, # ) code_url["cos_bucket_name"] = default_bucket_name.replace("-" + UserConfig().appid, '') \ if default_bucket_name and default_bucket_name.endswith( "-" + UserConfig().appid) else default_bucket_name code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() except Exception as e: cos_bucket_status = e # cos_bucket_status = 2 if cos_bucket_status not in (0, 1): size_infor = self.file_size_infor(file_size) if size_infor == -1: Operation("Upload Error.").warning() raise UploadFailed(str(e)) else: Operation("There are some exceptions and the process of uploading to COS is terminated!").warning() if len(str(cos_bucket_status)) > 3: Operation(str(cos_bucket_status)).warning() Operation("This package will be uploaded by TencentCloud Cloud API.").information() if size_infor == 0: msg = "Package size is over 8M, it is highly recommended that you upload using COS. " Operation(msg).information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() else: msg = "If you want to increase the upload speed, you can configure using-cos with command:scf configure set" Operation(msg).information() size_infor = self.file_size_infor(file_size) if size_infor == -1: msg = 'Your package is too large and needs to be uploaded via COS.' Operation(msg).warning() msg = 'You can use --cos-bucket BucketName to specify the bucket, or you can use the "scf configure set" to set the default to open the cos upload.' Operation(msg).warning() raise UploadFailed("Upload faild") elif size_infor == 0: Operation("Package size is over 8M, it is highly recommended that you upload using COS. ").information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() return code_url
def _do_package_core(self, func_path, namespace, func_name, region=None): zipfile, zip_file_name, zip_file_name_cos = self._zip_func( func_path, namespace, func_name) code_url = dict() file_size = os.path.getsize( os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name)) Operation("Package name: %s, package size: %s kb" % (zip_file_name, str(file_size / 1000))).process() default_bucket_name = "" if UserConfig().using_cos.startswith("True"): cos_bucket_status = True default_bucket_name = "scf-deploy-" + region + "-" + str( UserConfig().appid) else: cos_bucket_status = False if self.without_cos: self.file_size_infor(file_size) Operation("Uploading this package without COS.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() elif self.cos_bucket: bucket_name = self.cos_bucket + "-" + UserConfig().appid Operation("Uploading this package to COS, bucket_name: %s" % (bucket_name)).process() CosClient(region).upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name_cos) Operation("Upload success").success() code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() elif cos_bucket_status: Operation("By default, this package will be uploaded to COS." ).information() Operation("Default COS-bucket: " + default_bucket_name).information() Operation("If you don't want to upload the package to COS by default, you could change your configure!") \ .information() # 根据region设置cos_client cos_client = CosClient(region) Operation("Checking you COS-bucket.").process() # 获取COS bucket cos_bucket_status = cos_client.get_bucket(default_bucket_name) if cos_bucket_status == -1: Operation("reating default COS-bucket: " + default_bucket_name).process() create_status = cos_client.create_bucket( bucket=default_bucket_name) if create_status == True: cos_bucket_status = 0 Operation("Creating success.").success() else: try: if "<?xml" in str(create_status): error_code = re.findall("<Code>(.*?)</Code>", str(create_status))[0] error_message = re.findall( "<Message>(.*?)</Message>", str(create_status))[0] Operation( "COS client error code: %s, message: %s" % (error_code, error_message)).warning() finally: cos_bucket_status = create_status Operation("Creating faild.").warning() if cos_bucket_status != 0: Operation( "There are some exceptions and the process of uploading to COS is terminated!" ).warning() Operation( "This package will be uploaded by TencentCloud Cloud API." ).information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() else: # 获取bucket正常,继续流程 file_data = zipfile.read() md5 = hashlib.md5(file_data).hexdigest() is_have = 0 try: object_list = cos_client.get_object_list( bucket=default_bucket_name, prefix=str(namespace) + "-" + str(func_name)) if isinstance(object_list, dict) and 'Contents' in object_list: for eve_object in object_list["Contents"]: if md5 in eve_object["ETag"]: response = cos_client.copy_object( default_bucket_name, eve_object["Key"], zip_file_name_cos, ) is_have = 1 break except Exception as e: pass if is_have == 0: Operation("Uploading to COS, bucket_name:" + default_bucket_name).process() cos_client.upload_file2cos(bucket=default_bucket_name, file=file_data, key=zip_file_name_cos) # cos_client.upload_file2cos2( # bucket=default_bucket_name, # file=os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name), # key=zip_file_name_cos, # md5=md5, # ) code_url["cos_bucket_name"] = default_bucket_name.replace("-" + UserConfig().appid, '') \ if default_bucket_name and default_bucket_name.endswith( "-" + UserConfig().appid) else default_bucket_name code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() else: Operation( \ "If you want to increase the upload speed, you can configure using-cos with command:scf configure set") \ .information() self.file_size_infor(file_size) Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() return code_url