【问题标题】:Azure PHP SDK: get the asset by asset nameAzure PHP SDK:按资产名称获取资产
【发布时间】:2015-09-29 06:16:03
【问题描述】:

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

getAssetList();

我可以使用资产 ID 获取单个资产详细信息。

getAsset($asset);

但就我而言,我没有资产 ID。我只有资产名称。现在如何使用它获取资产详细信息?

编辑:

我从 Azure 支持那里得到了一些帮助,说我们可以使用 $skip 参数进行分页。我在 c# 中得到了代码 sn-p

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 中使用这个参数。

【问题讨论】:

    标签: php azure sdk azure-blob-storage


    【解决方案1】:

    Azure SDK for PHP 似乎不支持跳过方法。但是,我使用 fiddler 来监控 C# skip 方法并得到这样的 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);
    

    【讨论】:

    • 太完美了。非常感谢:)
    【解决方案2】:

    Azure 媒体服务支持按名称筛选。你可以构造 web 请求就像

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

    您还可以按其他属性过滤

    【讨论】:

      【解决方案3】:

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

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

      但就我而言,我没有资产 ID。我只有资产名称 独自的。现在如何使用它获取资产详细信息?

      这里有一些测试功能代码sn-ps供大家参考:

      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

      【讨论】:

      • 我尝试了这种方法,但是当我尝试创建具有相同名称的新资产时,会创建新容器。在这种情况下,我无法获取已经创建的容器 SAS url。
      • 是否可以在您的帖子中提供一个代码 sn-p 以便我们方便地重现您的问题 [创建具有相同名称的新资产新容器已创建。]?目前,我无法完全理解您的根本原因。
      【解决方案4】:

      根据我的测试,似乎无法使用 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)”的方法。实际上,该方法通过 Asset id 获取 Asset 信息,就像 Aka's reference code 一样。Azure REST API 不支持通过 Asset 名称查询的方法。 请参考official document

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

      【讨论】:

      • 是的,我可以保存它以备将来使用。但不幸的是,我错过了他们的存储。所以现在我有2000多个资产。是否有任何其他工作可以完成这项工作?
      • 正如我在问题编辑中提到的,我从 Azure 支持获得了一些帮助,可以使用 $skip 进行分页。但我找不到任何 PHP 文档。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多