title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Copying blobs from a storage account into an Azure Media Services asset | Microsoft Docs |
This topic shows how to copy an existing blob into a Media Services Asset. The example uses Azure Media Services .NET SDK Extensions. |
media-services |
Juliako |
cfowler |
6a63823f-f3c9-424c-91b8-566f70bec346 |
media-services |
media |
na |
ne |
article |
08/03/2017 |
juliako |
This topic shows how to copy blobs from a storage account into a new Azure Media Services (AMS) asset using Azure Media Services .NET SDK Extensions.
The extension methods works with:
- Regular assets.
- Live archive assets (FragBlob format).
- Source and destination assets belonging to different Media Services accounts (even across different data centers). However, there may be charges incurred by doing so. For more information about pricing, see Data Transfers.
Note
You should not attempt to change the contents of blob containers that were generated by Media Services without using Media Service APIs.
The topic shows two code samples:
- Copy blobs from an asset in one AMS account into a new asset in another AMS account.
- Copy blobs from some storage account into a new asset in an AMS account.
Two Media Services accounts. See the topic How to Create a Media Services Account.
You can follow the steps in this article or you can download a sample that contains the code described in this article from here.
- Set up your development environment as described in Media Services development with .NET.
- Add other references that are required for this project: System.Configuration.
- Add the appSettings section to the .config file and update the values based on your Media Services accounts, the destination storage account and the source asset ID.
<appSettings>
<add key="AMSSourceAADTenantDomain" value="AADTenantDomain"/>
<add key="AMSSourceRESTAPIEndpoint" value="RESTAPIEndpoint"/>
<add key="AMSDestAADTenantDomain" value="AADTenantDomain"/>
<add key="AMSDestRESTAPIEndpoint" value="RESTAPIEndpoint"/>
<add key="DestStorageAccountName" value="name"/>
<add key="DestStorageAccountKey" value="key"/>
<add key="SourceAssetID" value="nb:cid:UUID:assetID"/>
</appSettings>
The following code uses extension IAsset.Copy method to copy all files in the source asset into the destination asset using a single extension.
using System;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Linq;
using System.Configuration;
using Microsoft.WindowsAzure.Storage.Auth;
namespace CopyExistingBlobsIntoAsset
{
class Program
{
static string _sourceAADTenantDomain = ConfigurationManager.AppSettings["AMSSourceAADTenantDomain"];
static string _sourceRESTAPIEndpoint = ConfigurationManager.AppSettings["AMSSourceRESTAPIEndpoint"];
static string _destAADTenantDomain = ConfigurationManager.AppSettings["AMSDestAADTenantDomain"];
static string _destRESTAPIEndpoint = ConfigurationManager.AppSettings["AMSDestRESTAPIEndpoint"];
static string _destStorageAccountName = ConfigurationManager.AppSettings["DestStorageAccountName"];
static string _destStorageAccountKey = ConfigurationManager.AppSettings["DestStorageAccountKey"];
static string _sourceAssetID = ConfigurationManager.AppSettings["SourceAssetID"];
private static CloudMediaContext _sourceContext = null;
private static CloudMediaContext _destContext = null;
static void Main(string[] args)
{
var tokenCredentials1 = new AzureAdTokenCredentials(_sourceAADTenantDomain, AzureEnvironments.AzureCloudEnvironment);
var tokenProvider1 = new AzureAdTokenProvider(tokenCredentials1);
var tokenCredentials2 = new AzureAdTokenCredentials(_destAADTenantDomain, AzureEnvironments.AzureCloudEnvironment);
var tokenProvider2 = new AzureAdTokenProvider(tokenCredentials2);
// Create the context for your source Media Services account.
_sourceContext = new CloudMediaContext(new Uri(_sourceRESTAPIEndpoint), tokenProvider1);
// Create the context for your destination Media Services account.
_destContext = new CloudMediaContext(new Uri(_destRESTAPIEndpoint), tokenProvider2);
// Get the credentials of the default Storage account bound to your destination Media Services account.
StorageCredentials destinationStorageCredentials =
new StorageCredentials(_destStorageAccountName, _destStorageAccountKey);
// Get a reference to the source asset in the source context.
IAsset sourceAsset = _sourceContext.Assets.Where(a => a.Id == _sourceAssetID).First();
// Create an empty destination asset in the destination context.
IAsset destinationAsset = _destContext.Assets.Create(sourceAsset.Name, AssetCreationOptions.None);
// Copy the files in the source asset instance into the destination asset instance.
sourceAsset.Copy(destinationAsset, destinationStorageCredentials);
Console.WriteLine("Done");
}
}
}
- One Storage account from which you want to copy blobs.
- One AMS account into which you want to copy blobs.
- Set up your development environment as described in Media Services development with .NET.
- Add other references that are required for this project: System.Configuration.
- Add the appSettings section to the .config file and update the values based on your source storage and destination AMS accounts.
<appSettings>
<add key="SourceStorageAccountName" value="name" />
<add key="SourceStorageAccountKey" value="key" />
<add key="AMSAADTenantDomain" value="tenant"/>
<add key="AMSESTAPIEndpoint" value="endpoint"/>
<add key="AMSStorageAccountName" value="name" />
<add key="AMSStorageAccountKey" value="key" />
</appSettings>
The following code copies blobs from a storage account into a Media Services asset.
Note
There is a limit of 1,000,000 policies for different AMS policies (for example, for Locator policy or ContentKeyAuthorizationPolicy). You should use the same policy ID if you are always using the same days / access permissions, for example, policies for locators that are intended to remain in place for a long time (non-upload policies). For more information, see this topic.
using System;
using System.Configuration;
using System.Linq;
using Microsoft.WindowsAzure.MediaServices.Client;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace CopyExistingBlobsIntoAsset
{
class Program
{
// Read values from the App.config file.
private static readonly string _AMSAADTenantDomain =
ConfigurationManager.AppSettings["AMSAADTenantDomain"];
private static readonly string _AMSRESTAPIEndpoint =
ConfigurationManager.AppSettings["AMSESTAPIEndpoint"];
private static readonly string _AMSStorageAccountName =
ConfigurationManager.AppSettings["AMSStorageAccountName"];
private static readonly string _AMSStorageAccountKey =
ConfigurationManager.AppSettings["AMSStorageAccountKey"];
private static readonly string _sourceStorageAccountName =
ConfigurationManager.AppSettings["SourceStorageAccountName"];
private static readonly string _sourceStorageAccountKey =
ConfigurationManager.AppSettings["SourceStorageAccountKey"];
// Field for service context.
private static CloudMediaContext _context = null;
private static CloudStorageAccount _sourceStorageAccount = null;
private static CloudStorageAccount _destinationStorageAccount = null;
static void Main(string[] args)
{
var tokenCredentials = new AzureAdTokenCredentials(_AMSAADTenantDomain,
AzureEnvironments.AzureCloudEnvironment);
var tokenProvider = new AzureAdTokenProvider(tokenCredentials);
// Create the context for your source Media Services account.
_context = new CloudMediaContext(new Uri(_AMSRESTAPIEndpoint), tokenProvider);
_sourceStorageAccount =
new CloudStorageAccount(new StorageCredentials(_sourceStorageAccountName,
_sourceStorageAccountKey), true);
_destinationStorageAccount =
new CloudStorageAccount(new StorageCredentials(_AMSStorageAccountName,
_AMSStorageAccountKey), true);
CloudBlobClient sourceCloudBlobClient =
_sourceStorageAccount.CreateCloudBlobClient();
CloudBlobContainer sourceContainer =
sourceCloudBlobClient.GetContainerReference("NameOfBlobContainerYouWantToCopy");
CreateAssetFromExistingBlobs(sourceContainer);
Console.WriteLine("Done");
}
static public IAsset CreateAssetFromExistingBlobs(CloudBlobContainer sourceBlobContainer)
{
CloudBlobClient destBlobStorage = _destinationStorageAccount.CreateCloudBlobClient();
// Create a new asset.
IAsset asset = _context.Assets.Create("NewAsset_" + Guid.NewGuid(), AssetCreationOptions.None);
IAccessPolicy writePolicy = _context.AccessPolicies.Create("writePolicy",
TimeSpan.FromHours(24), AccessPermissions.Write);
ILocator destinationLocator =
_context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);
// Get the asset container URI and Blob copy from mediaContainer to assetContainer.
CloudBlobContainer destAssetContainer =
destBlobStorage.GetContainerReference((new Uri(destinationLocator.Path)).Segments[1]);
if (destAssetContainer.CreateIfNotExists())
{
destAssetContainer.SetPermissions(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
var blobList = sourceBlobContainer.ListBlobs();
foreach (var sourceBlob in blobList)
{
var assetFile = asset.AssetFiles.Create((sourceBlob as ICloudBlob).Name);
ICloudBlob destinationBlob = destAssetContainer.GetBlockBlobReference(assetFile.Name);
CopyBlob(sourceBlob as ICloudBlob, destAssetContainer);
assetFile.ContentFileSize = (sourceBlob as ICloudBlob).Properties.Length;
assetFile.Update();
Console.WriteLine("File {0} is of {1} size", assetFile.Name, assetFile.ContentFileSize);
}
asset.Update();
destinationLocator.Delete();
writePolicy.Delete();
// Set the primary asset file.
// If, for example, we copied a set of Smooth Streaming files,
// set the .ism file to be the primary file.
// If we, for example, copied an .mp4, then the mp4 would be the primary file.
var ismAssetFile = asset.AssetFiles.ToList().
Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).ToArray().FirstOrDefault();
// The following code assigns the first .ism file as the primary file in the asset.
// An asset should have one .ism file.
if (ismAssetFile != null)
{
ismAssetFile.IsPrimary = true;
ismAssetFile.Update();
}
return asset;
}
/// <summary>
/// Copies the specified blob into the specified container.
/// </summary>
/// <param name="sourceBlob">The source container.</param>
/// <param name="destinationContainer">The destination container.</param>
static private void CopyBlob(ICloudBlob sourceBlob, CloudBlobContainer destinationContainer)
{
var signature = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});
ICloudBlob destinationBlob = destinationContainer.GetBlockBlobReference(sourceBlob.Name);
if (destinationBlob.Exists())
{
Console.WriteLine(string.Format("Destination blob '{0}' already exists. Skipping.", destinationBlob.Uri));
}
else
{
// Display the size of the source blob.
Console.WriteLine(sourceBlob.Properties.Length);
Console.WriteLine(string.Format("Copy blob '{0}' to '{1}'", sourceBlob.Uri, destinationBlob.Uri));
destinationBlob.StartCopyFromBlob(new Uri(sourceBlob.Uri.AbsoluteUri + signature));
while (true)
{
// The StartCopyFromBlob is an async operation,
// so we want to check if the copy operation is completed before proceeding.
// To do that, we call FetchAttributes on the blob and check the CopyStatus.
destinationBlob.FetchAttributes();
if (destinationBlob.CopyState.Status != CopyStatus.Pending)
{
break;
}
//It's still not completed. So wait for some time.
System.Threading.Thread.Sleep(1000);
}
// Display the size of the destination blob.
Console.WriteLine(destinationBlob.Properties.Length);
}
}
}
}
You can now encode your uploaded assets. For more information, see Encode assets.
You can also use Azure Functions to trigger an encoding job based on a file arriving in the configured container. For more information, see this sample.
[!INCLUDE media-services-learning-paths-include]
[!INCLUDE media-services-user-voice-include]