def clean_and_delete_bucket(bucket): # check if bucket is in versioning status try: result = bucket.get_bucket_info() if result.versioning_status in [oss2.BUCKET_VERSIONING_ENABLE, oss2.BUCKET_VERSIONING_SUSPEND]: all_objects = bucket.list_object_versions() for obj in all_objects.versions: bucket.delete_object(obj.key, params={'versionId': obj.versionid}) except: pass # list all upload_parts to delete up_iter = oss2.MultipartUploadIterator(bucket) for up in up_iter: bucket.abort_multipart_upload(up.key, up.upload_id) # list all objects to delete obj_iter = oss2.ObjectIterator(bucket) for obj in obj_iter: bucket.delete_object(obj.key) # list all live channels to delete for ch_iter in oss2.LiveChannelIterator(bucket): bucket.delete_live_channel(ch_iter.name) # delete_bucket bucket.delete_bucket()
def test_live_channel_iterator(self): prefix = self.random_key() channel_name_list = [] channel_target = oss2.models.LiveChannelInfoTarget( playlist_name='test.m3u8') channel_info = oss2.models.LiveChannelInfo(target=channel_target) # 准备频道 for i in range(20): channel_name_list.append(prefix + random_string(16)) self.bucket.create_live_channel(channel_name_list[-1], channel_info) # 验证 live_channel_got = [] for info in oss2.LiveChannelIterator(self.bucket, prefix, max_keys=4): live_channel_got.append(info.name) result = self.bucket.get_live_channel(info.name) self.assertEqual(result.description, info.description) self.assertEqual(sorted(channel_name_list), live_channel_got) for live_channel in channel_name_list: self.bucket.delete_live_channel(live_channel)
def clean_and_delete_bucket(bucket): # check if bucket is in versioning status try: result = bucket.get_bucket_info() if result.versioning_status in [ oss2.BUCKET_VERSIONING_ENABLE, oss2.BUCKET_VERSIONING_SUSPEND ]: next_key_marker = None next_versionid_marker = None is_truncated = True while is_truncated is True: objects = bucket.list_object_versions( key_marker=next_key_marker, versionid_marker=next_versionid_marker) for obj in objects.versions: bucket.delete_object(obj.key, params={'versionId': obj.versionid}) for del_marker in objects.delete_marker: bucket.delete_object( del_marker.key, params={'versionId': del_marker.versionid}) is_truncated = objects.is_truncated if is_truncated: next_key_marker = objects.next_key_marker next_versionid_marker = objects.next_versionid_marker except: pass # list all upload_parts to delete up_iter = oss2.MultipartUploadIterator(bucket) for up in up_iter: bucket.abort_multipart_upload(up.key, up.upload_id) # list all objects to delete obj_iter = oss2.ObjectIterator(bucket) for obj in obj_iter: bucket.delete_object(obj.key) # list all live channels to delete for ch_iter in oss2.LiveChannelIterator(bucket): bucket.delete_live_channel(ch_iter.name) # delete_bucket bucket.delete_bucket()
playlist_name=playlist_name, frag_count=3, frag_duration=5))) #获取推流观流地址 publish_url = create_result.publish_url play_url = create_result.play_url print("推流地址:", publish_url) print("观流地址:", play_url) '推流地址: rtmp://***-channel.oss-cn-***.aliyuncs.com/**/**-**' '观流地址: http://***-channel.oss-cn-****.aliyuncs.com/song-**/**.m3u8' prefix = '' max_keys = 1000 #for info in oss2.LiveChannelIterator(bucket, prefix, max_keys=max_keys): for info in oss2.LiveChannelIterator(bucket): print(info.name) bucket.delete_live_channel(info.name) #查看当前流的状态信息状态 get_statu = bucket.get_live_channel_stat(info.name) print("连接时间:", get_statu.connected_time) print("推流客户端的IP:", get_statu.remote_addr) print("推流状态:", get_statu.status) #查看LiveChannel配置信息 get_result = bucket.get_live_channel(info.name) print("-------------------") print("推流配置信息:") print(get_result.description)
# 拿到这个签过名的signed_url就可以使用推流工具直接进行推流,一旦连接上OSS之后超过上面的expires流也不会断掉,OSS仅在每次推流连接的时候检查expires是否合法。 expires = 3600 signed_url = bucket.sign_rtmp_url(channel_name, playlist_name, expires) # 创建好直播频道,如果想把这个频道禁用掉(断掉正在推的流或者不再允许向一个地址推流),应该使用put_live_channel_status接口,将频道的status改成“disabled”,如果要将一个禁用状态的频道启用,那么也是调用这个接口,将status改成“enabled”。 bucket.put_live_channel_status(channel_name, 'enabled') bucket.put_live_channel_status(channel_name, 'disabled') # 对创建好的频道,可以使用LiveChannelIterator来进行列举已达到管理的目的。 # prefix可以按照前缀过滤list出来的频道。 # max_keys表示迭代器内部一次list出来的频道的最大数量,这个值最大不能超过1000,不填写的话默认为100。 prefix = '' max_keys = 1000 for info in oss2.LiveChannelIterator(bucket, prefix, max_keys=max_keys): print(info.name) # 对于正在推流的频道调用get_live_channel_stat可以获得流的状态信息。 # 如果频道正在推流,那么stat_result中的所有字段都有意义。 # 如果频道闲置或者处于“disabled”状态,那么status为“Idle”或“Disabled”,其他字段无意义。 stat_result = bucket.get_live_channel_stat(channel_name) print(stat_result.status) print(stat_result.remote_addr) print(stat_result.connected_time) print(stat_result.video) print(stat_result.audio) # 如果想查看一个频道历史推流记录,可以调用get_live_channel_history。目前最多可以看到10次推流的记录 history_result = bucket.get_live_channel_history(channel_name) print(len(history_result.records))