Advertisement
Guest User

Untitled

a guest
May 25th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1.  
  2. /// <summary>
  3. /// This class uploads files to Azure.
  4. /// </summary>
  5. public class SimpleAzureUploader
  6. {
  7. private readonly CloudMediaContext _context;
  8.  
  9. /// <summary>
  10. /// Initializes a new instance of the <see cref="SimpleAzureUploader"/> class.
  11. /// </summary>
  12. /// <param name="accountName">Name of the Azure Media Services account.</param>
  13. /// <param name="accountKey">The Account key.</param>
  14. public SimpleAzureUploader(string accountName, string accountKey)
  15. {
  16. _context = new CloudMediaContext(accountName, accountKey);
  17. }
  18.  
  19. /// <summary>
  20. /// Uplodas a local folder to a non-encrypted asset in Azure.
  21. /// </summary>
  22. /// <param name="assetName">Name of the asset.</param>
  23. /// <param name="path">The directory or file path.</param>
  24. /// <param name="concurrentTransfers">The max number of concurrent transfers.</param>
  25. /// <param name="parallelTransferThreads">The max number parallel transfer threads.</param>
  26. /// <returns></returns>
  27. public IAsset UploadAsset(string assetName, string path, int concurrentTransfers = 10, int parallelTransferThreads = 10)
  28. {
  29. var blobTransferClient = new BlobTransferClient
  30. {
  31. NumberOfConcurrentTransfers = concurrentTransfers,
  32. ParallelTransferThreadCount = parallelTransferThreads
  33. };
  34.  
  35. var asset = _context.Assets.Create(assetName, AssetCreationOptions.None);
  36.  
  37. var uploadingAccessPolicy = _context.AccessPolicies.Create("Upload Policy", new TimeSpan(1, 0, 0, 0), AccessPermissions.Write | AccessPermissions.List);
  38.  
  39. var uploadingLocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, uploadingAccessPolicy);
  40.  
  41. if (Directory.Exists(path) && new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory))
  42. {
  43. UploadFolder(asset, uploadingLocator, path, blobTransferClient);
  44. }
  45. else
  46. {
  47. UploadFile(asset, uploadingLocator, path, blobTransferClient);
  48. }
  49.  
  50. uploadingLocator.Delete();
  51. uploadingAccessPolicy.Delete();
  52.  
  53. return asset;
  54. }
  55.  
  56. /// <summary>
  57. /// Uploads the folder to the existing asset.
  58. /// </summary>
  59. /// <param name="asset">The existing asset.</param>
  60. /// <param name="locator">The uploading locator.</param>
  61. /// <param name="folderPath">The folder path.</param>
  62. /// <param name="blobTransferClient">The blob transfer client.</param>
  63. /// <exception cref="System.IO.FileNotFoundException"></exception>
  64. private void UploadFolder(IAsset asset, ILocator locator, string folderPath, BlobTransferClient blobTransferClient)
  65. {
  66. var filePaths = Directory.GetFiles(folderPath);
  67.  
  68. if (!filePaths.Any())
  69. {
  70. throw new FileNotFoundException(String.Format("No files in directory, check folderPath: {0}", folderPath));
  71. }
  72.  
  73. Task.WaitAll((from filePath
  74. in filePaths
  75. let assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath))
  76. select assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None)).ToArray());
  77. }
  78.  
  79. /// <summary>
  80. /// Uploads the file to the existing asset.
  81. /// </summary>
  82. /// <param name="asset">The existing asset.</param>
  83. /// <param name="locator">The uploading locator.</param>
  84. /// <param name="filePath">The file path.</param>
  85. /// <param name="blobTransferClient">The blob transfer client.</param>
  86. private void UploadFile(IAsset asset, ILocator locator, string filePath, BlobTransferClient blobTransferClient)
  87. {
  88. var assetFile = asset.AssetFiles.Create(Path.GetFileName(filePath));
  89. assetFile.UploadAsync(filePath, blobTransferClient, locator, CancellationToken.None).Wait();
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement