Azure PHP SDK:根据资源名称获取资源


Azure PHP SDK: get the asset by asset name

是否可以使用Azure PHP sdk的资产名称获取资产详细信息。我可以得到所有的资源列表,但它只加载前1000个资源。

getAssetList();

我可以使用资产id获得单个资产的详细信息。

getAsset($asset);

但在我的情况下,我没有资产id。我只有资产名。现在我如何使用这个获得资产细节呢?

编辑:

我从Azure支持人员那里得到了一些帮助,说我们可以使用$skip参数进行分页。我得到了c#中的代码片段

for (int i = 0; i < _context.Assets.Count(); i += 1000 )
{
    var assets = _context.Assets.Skip(i);
    foreach (IAsset objIAsset in assets)
    {
        Console.WriteLine(objIAsset.Name.ToString());
    }
}
如何在PHP SDK中使用此参数

Azure SDK for PHP似乎不支持跳过方法。然而,我使用fiddler来监视c#跳过方法,并得到这样的URL:

https://***-hs.cloudapp.net/api/Assets()?$skip=1000

所以我认为我们可以像上面那样在我们的PHP项目中建立请求路径,我们可以在"MediaServicesRestProxy"文件中修改getAssetList方法。

我在"MediaServicesRestProxy"类中添加了一个名为"getAssetListBySkip($number)"的函数,代码如下:

/**
 * Get asset list using skip number
 * 
 * */
public function getAssetListBySkip($number)
{
    $propertyList = $this->_getEntityList("Assets()?".'$skip='.$number);
    $result       = array();
    foreach ($propertyList as $properties) {
        $result[] = Asset::createFromOptions($properties);
    }
    return $result;
}

可以这样调用这个方法:

$mediaServiceProxy = ServicesBuilder::getInstance()->createMediaServicesService(
        new MediaServicesSettings("**","**/**="));
$result=$mediaServiceProxy->getAssetListBySkip(1000);

Azure Media服务支持按名称过滤。你可以把web请求写成

/api/assets()?$filter=Name%20eq%20'Your Name'&$top=1 

也可以通过其他属性进行筛选

您是否尝试过创建、处理、管理和交付资产时使用的REST API ?https://msdn.microsoft.com/en-us/library/azure/hh974277.aspx#list_an_asset但我认为我们可以通过名称直接列出资产,因为id是资产实体的唯一标识符。PHP Azure SDK也利用assetId来获取资产:

 public function getAsset($asset) 
     { 
         $assetId = Utilities::getEntityId( 
           $asset, 
             'WindowsAzure'MediaServices'Models'Asset' 
        ); 
         return Asset::createFromOptions($this->_getEntity("Assets('{$assetId}')")); 
     } 

但在我的情况下,我没有资产id。我只有资产名一个人。现在我如何使用这个获得资产细节呢?

这里有一些测试函数代码片段供您参考:

public function testListAllAssets(){ 
           // Setup 
         $asset1 = new Asset(Asset::OPTIONS_NONE); 
         $asset1->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix()); 
         $asset2 = new Asset(Asset::OPTIONS_NONE); 
         $asset2->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix()); 
         // Test 
         $asset1 = $this->createAsset($asset1); 
         $asset2 = $this->createAsset($asset2); 
         $result = $this->restProxy->getAssetList(); 
         // Assert 
         $this->assertCount(2, $result); 
         $names = array( 
             $result[0]->getName(), 
             $result[1]->getName() 
         ); 
         $id = array( 
                 $result[0]->getId(), 
                 $result[1]->getId() 
         ); 
         $this->assertContains($asset1->getName(), $names); 
         $this->assertContains($asset2->getName(), $names); 
         $this->assertContains($asset1->getId(), $id); 
         $this->assertContains($asset2->getId(), $id); 
     } 
 public function testGetAnAssetReference(){ 
         // Setup 
         $assetName = TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix(); 
         $asset = new Asset(Asset::OPTIONS_NONE); 
         $asset->setName($assetName); 
         $asset = $this->createAsset($asset); 
         // Test 
         $result = $this->restProxy->getAsset($asset); 
         // Assert 
         $this->assertEquals($asset->getId(), $result->getId()); 
         $this->assertEquals($asset->getName(), $result->getName()); 
     } 

从:https://github.com/Azure/azure-sdk-for-php/blob/master/tests/functional/WindowsAzure/MediaServices/MediaServicesFunctionalTest.php

根据我的测试,我们似乎无法使用Asset的名称来获取Media Service中资产的信息。

$mediaServiceProxy = ServicesBuilder::getInstance()->createMediaServicesService(
              new MediaServicesSettings("**","******"));
$asset = new Asset(Asset::OPTIONS_NONE);
$asset->setName('For-Test-wmv-Source');
//$asset don't have the value of id,
// unless execute ‘createAsset($asset)’, "$asset1" will be set the ID 
$asset1 =$mediaServiceProxy->createAsset($asset);
$result2=$mediaServiceProxy->getAsset($asset1); 
PHP SDK支持名为"getAsset($asset)"的方法。实际上,该方法通过资产id获取资产信息,就像Aka's reference code一样。Azure REST API不支持通过资产名称查询的方法。请以官方文件为准。

另一种方法是,当您将资产信息(如Id,URl,名称等)上传到媒体服务时,您可以将其存储在Azure table storage中。如果你想使用它,你可以从table storage中获取并过滤你想要的资产名称的数据。