/// <summary> /// Generates the URIs that a client can use to upload images to the storage system /// </summary> /// <param name="entrantId">The entrant that is being uploaded</param> /// <param name="generateDisplayUri">Controls the generation of the displayUploadUri</param> /// <param name="verifyContainerName">The name of the blob container for verification images. Default: 'photos-verification'</param> /// <param name="displayContainerName">The name of the blob container for verification images. Default: 'photos-display'</param> /// <returns></returns> public async Task <(Uri verifyUploadUri, Uri?displayUploadUri)> GenerateImageUploadUris(string entrantId, bool generateDisplayUri, string verifyContainerName = "photos-verification", string displayContainerName = "photos-display") { if (string.IsNullOrWhiteSpace(entrantId)) { throw new ArgumentNullException(nameof(entrantId)); } //Generate containers var blobClient = new BlobContainerClient(_storageAccount.ToString(true), displayContainerName); await blobClient.CreateIfNotExistsAsync(PublicAccessType.Blob).ConfigureAwait(false); blobClient = new BlobContainerClient(_storageAccount.ToString(true), verifyContainerName); await blobClient.CreateIfNotExistsAsync(PublicAccessType.None).ConfigureAwait(false); var sasBuilder = new BlobSasBuilder { BlobContainerName = verifyContainerName, BlobName = entrantId, Resource = @"b", ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5) }; //Sets the permissions for the SAS token sasBuilder.SetPermissions(BlobSasPermissions.Create | BlobSasPermissions.Write | BlobSasPermissions.Delete); var credentials = new StorageSharedKeyCredential(_storageAccount.Credentials.AccountName, _storageAccount.Credentials.ExportBase64EncodedKey()); //The constructed sas token var verifyUploadSas = sasBuilder.ToSasQueryParameters(credentials); //Uri builder for verify blob var builder = new BlobUriBuilder(_storageAccount.BlobEndpoint) { BlobName = entrantId, BlobContainerName = verifyContainerName, AccountName = _storageAccount.Credentials.AccountName, Sas = verifyUploadSas }; var verifyUploadUri = builder.ToUri(); sasBuilder.BlobContainerName = displayContainerName; builder.BlobContainerName = displayContainerName; if (generateDisplayUri) { builder.Sas = sasBuilder.ToSasQueryParameters(credentials); var displayUploadUri = builder.ToUri(); return(verifyUploadUri, displayUploadUri); } return(verifyUploadUri, null); }
public void WithVersion() { // Arrange string accountName = "accountname"; string containerName = GetNewContainerName(); string blobName = "my/blob/name"; string versionId = "2020-07-03T12:45:46.1234567Z"; Uri uri = new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{Uri.EscapeDataString(blobName)}"); Uri versionUri = new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{Uri.EscapeDataString(blobName)}?versionid={versionId}"); // Act BlobClient blobClient = new BlobClient(uri); BlobClient versionBlobClient = blobClient.WithVersion(versionId); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(versionBlobClient.Uri); // Assert Assert.AreEqual(accountName, versionBlobClient.AccountName); Assert.AreEqual(containerName, versionBlobClient.BlobContainerName); Assert.AreEqual(blobName, versionBlobClient.Name); Assert.AreEqual(versionUri, versionBlobClient.Uri); Assert.AreEqual(accountName, blobUriBuilder.AccountName); Assert.AreEqual(containerName, blobUriBuilder.BlobContainerName); Assert.AreEqual(blobName, blobUriBuilder.BlobName); Assert.AreEqual(versionId, blobUriBuilder.VersionId); Assert.AreEqual(versionUri, blobUriBuilder.ToUri()); }
//------------------------------------------------- // Restore a previous version //------------------------------------------------- private static void CopyVersionToBaseBlob(string blobName) { var connectionString = Constants.connectionString; BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); BlobContainerClient container = blobServiceClient.GetBlobContainerClient(Constants.containerName); // Get a specific blob to restore. BlobClient blockBlob = container.GetBlobClient(blobName); // <Snippet_RestorePreviousVersion> // List blobs in this container that match prefix. // Include versions in listing. Pageable <BlobItem> blobItems = container.GetBlobs (BlobTraits.None, BlobStates.Version, prefix: blockBlob.Name); // Get the URI for the most recent version. BlobUriBuilder blobVersionUri = new BlobUriBuilder(blockBlob.Uri) { VersionId = blobItems .OrderByDescending(version => version.VersionId) .ElementAtOrDefault(1)?.VersionId }; // Restore the most recently generated version by copying it to the base blob. blockBlob.StartCopyFromUri(blobVersionUri.ToUri()); // </Snippet_RestorePreviousVersion> }
public async Task AccountSas_AllPermissions() { // Arrange await using DisposingContainer test = await GetTestContainerAsync(); string blobName = GetNewBlobName(); AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(blobName)); await blob.CreateAsync(); AccountSasBuilder accountSasBuilder = new AccountSasBuilder( permissions: AccountSasPermissions.All, expiresOn: Recording.UtcNow.AddDays(1), services: AccountSasServices.Blobs, resourceTypes: AccountSasResourceTypes.Object); Uri accountSasUri = test.Container.GetParentBlobServiceClient().GenerateAccountSasUri(accountSasBuilder); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(accountSasUri) { BlobContainerName = test.Container.Name, BlobName = blobName }; // Assert AppendBlobClient sasBlobClient = InstrumentClient(new AppendBlobClient(blobUriBuilder.ToUri(), GetOptions())); await sasBlobClient.GetPropertiesAsync(); }
public async Task SetPermissions_BlobContainerSasPermissions(BlobContainerSasPermissions permissions) { // Arrange await using DisposingContainer test = await GetTestContainerAsync(); BlobSasBuilder blobSasBuilder = new BlobSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), BlobContainerName = test.Container.Name }; blobSasBuilder.SetPermissions(permissions); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigDefault.AccountName, TestConfigDefault.AccountKey); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(test.Container.Uri) { Sas = blobSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; BlobContainerClient sasContainerClient = new BlobContainerClient(blobUriBuilder.ToUri(), GetOptions()); // Act await foreach (BlobItem blobItem in sasContainerClient.GetBlobsAsync()) { // Just make sure the call succeeds. } }
public async Task SetPermissions_BlobSasPermissions(BlobSasPermissions permissions) { // Arrange await using DisposingContainer test = await GetTestContainerAsync(); BlobBaseClient blob = await GetNewBlobClient(test.Container); BlobSasBuilder blobSasBuilder = new BlobSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), BlobContainerName = test.Container.Name, BlobName = blob.Name }; blobSasBuilder.SetPermissions(permissions); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigDefault.AccountName, TestConfigDefault.AccountKey); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(blob.Uri) { Sas = blobSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; BlobBaseClient sasBlobClient = InstrumentClient(new BlobBaseClient(blobUriBuilder.ToUri(), GetOptions())); // Act await sasBlobClient.ExistsAsync(); }
public void WithSnapshot() { // Arrange string accountName = "accountname"; string containerName = GetNewContainerName(); string blobName = "my/blob/name"; string snapshot = "2020-07-03T12:45:46.1234567Z"; Uri uri = new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{Uri.EscapeDataString(blobName)}"); Uri snapshotUri = new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{Uri.EscapeDataString(blobName)}?snapshot={snapshot}"); // Act BlobClient blobClient = new BlobClient(uri); BlobClient snapshotBlobClient = blobClient.WithSnapshot(snapshot); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(snapshotBlobClient.Uri); // Assert Assert.AreEqual(accountName, snapshotBlobClient.AccountName); Assert.AreEqual(containerName, snapshotBlobClient.BlobContainerName); Assert.AreEqual(blobName, snapshotBlobClient.Name); Assert.AreEqual(snapshotUri, snapshotBlobClient.Uri); Assert.AreEqual(accountName, blobUriBuilder.AccountName); Assert.AreEqual(containerName, blobUriBuilder.BlobContainerName); Assert.AreEqual(blobName, blobUriBuilder.BlobName); Assert.AreEqual(snapshot, blobUriBuilder.Snapshot); Assert.AreEqual(snapshotUri, blobUriBuilder.ToUri()); }
public void BlobUriBuilder_IPStyleUrl_SasTest() { // Arrange var uriString = "https://127.0.0.1/account/container/blob?sv=2015-04-05&spr=https&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sip=168.1.5.60-168.1.5.70&sr=b&sp=rw&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D"; var originalUri = new UriBuilder(uriString); // Act var blobUriBuilder = new BlobUriBuilder(originalUri.Uri); Uri newUri = blobUriBuilder.ToUri(); // Assert Assert.AreEqual("https", blobUriBuilder.Scheme); Assert.AreEqual("127.0.0.1", blobUriBuilder.Host); Assert.AreEqual("account", blobUriBuilder.AccountName); Assert.AreEqual("container", blobUriBuilder.BlobContainerName); Assert.AreEqual("blob", blobUriBuilder.BlobName); Assert.AreEqual("", blobUriBuilder.Snapshot); Assert.AreEqual(new DateTimeOffset(2015, 4, 30, 2, 23, 26, TimeSpan.Zero), blobUriBuilder.Sas.ExpiresOn); Assert.AreEqual("", blobUriBuilder.Sas.Identifier); Assert.AreEqual(SasIPRange.Parse("168.1.5.60-168.1.5.70"), blobUriBuilder.Sas.IPRange); Assert.AreEqual("rw", blobUriBuilder.Sas.Permissions); Assert.AreEqual(SasProtocol.Https, blobUriBuilder.Sas.Protocol); Assert.AreEqual("b", blobUriBuilder.Sas.Resource); Assert.IsNull(blobUriBuilder.Sas.ResourceTypes); Assert.IsNull(blobUriBuilder.Sas.Services); Assert.AreEqual("Z/RHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk=", blobUriBuilder.Sas.Signature); Assert.AreEqual(new DateTimeOffset(2015, 4, 29, 22, 18, 26, TimeSpan.Zero), blobUriBuilder.Sas.StartsOn); Assert.AreEqual("2015-04-05", blobUriBuilder.Sas.Version); Assert.AreEqual("", blobUriBuilder.Query); Assert.AreEqual(443, blobUriBuilder.Port); Assert.AreEqual(originalUri, newUri); }
//------------------------------------------------- // Recover a specific blob snapshot //------------------------------------------------- private static async Task CopySnapshotToBaseBlob() { var connectionString = Constants.connectionString; BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); BlobContainerClient container = blobServiceClient.GetBlobContainerClient(Constants.containerName); // Get a specific blob to restore. BlobClient blockBlob = container.GetBlobClient("blob1.txt"); // <Snippet_RecoverSpecificBlobSnapshot> // Restore the deleted blob. await blockBlob.UndeleteAsync(); // List blobs in this container that match prefix. // Include snapshots in listing. Pageable <BlobItem> blobItems = container.GetBlobs (BlobTraits.None, BlobStates.Snapshots, prefix: blockBlob.Name); // Get the URI for the most recent snapshot. BlobUriBuilder blobSnapshotUri = new BlobUriBuilder(blockBlob.Uri) { Snapshot = blobItems .OrderByDescending(snapshot => snapshot.Snapshot) .ElementAtOrDefault(1)?.Snapshot }; // Restore the most recent snapshot by copying it to the blob. blockBlob.StartCopyFromUri(blobSnapshotUri.ToUri()); // </Snippet_RecoverSpecificBlobSnapshot> }
private async Task <Uri> GetUserDelegationSasBlob(string blobName) { Uri serviceUri = new Uri($"https://{_accountName}.blob.core.windows.net"); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(_accountName, _accountKey); BlobServiceClient blobServiceClient = new BlobServiceClient(serviceUri, sharedKeyCredential); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(_containerName); PageBlobClient pageBlobClient = containerClient.GetPageBlobClient(blobName); // Create a SAS token that's valid for 7 days. BlobSasBuilder blobSasBuilder = new BlobSasBuilder() { BlobContainerName = _containerName, BlobName = blobName, Resource = "b", ExpiresOn = DateTimeOffset.UtcNow.AddDays(7) }; blobSasBuilder.SetPermissions(BlobSasPermissions.Read); BlobUriBuilder sasUriBuilder = new BlobUriBuilder(containerClient.Uri) { Query = blobSasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString() }; Uri containerSasUri = sasUriBuilder.ToUri(); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(containerSasUri) { BlobName = pageBlobClient.Name }; return(blobUriBuilder.ToUri()); }
public async Task BlobSnapshotSas_AllPermissions() { // Arrange await using DisposingContainer test = await GetTestContainerAsync(); string blobName = GetNewBlobName(); AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(blobName)); await blob.CreateAsync(); Response <BlobSnapshotInfo> snapshotResponse = await blob.CreateSnapshotAsync(); BlobSasBuilder blobSasBuilder = new BlobSasBuilder() { ExpiresOn = Recording.UtcNow.AddDays(1), BlobContainerName = test.Container.Name, BlobName = blobName, Snapshot = snapshotResponse.Value.Snapshot }; blobSasBuilder.SetPermissions(SnapshotSasPermissions.All); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(blob.Uri) { Snapshot = snapshotResponse.Value.Snapshot, Sas = blobSasBuilder.ToSasQueryParameters(GetNewSharedKeyCredentials()) }; // Act AppendBlobClient sasBlobClient = InstrumentClient(new AppendBlobClient(blobUriBuilder.ToUri(), GetOptions())); await sasBlobClient.GetPropertiesAsync(); }
public async Task BlobVersionSas_AllPermissions() { // Arrange await using DisposingContainer test = await GetTestContainerAsync(); string blobName = GetNewBlobName(); AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(blobName)); Response <BlobContentInfo> createResponse = await blob.CreateAsync(); IDictionary <string, string> metadata = BuildMetadata(); Response <BlobInfo> metadataResponse = await blob.SetMetadataAsync(metadata); BlobSasBuilder blobSasBuilder = new BlobSasBuilder() { ExpiresOn = Recording.UtcNow.AddDays(1), BlobContainerName = test.Container.Name, BlobName = blobName, BlobVersionId = createResponse.Value.VersionId }; blobSasBuilder.SetPermissions(BlobVersionSasPermissions.All); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(blob.Uri) { VersionId = createResponse.Value.VersionId, Sas = blobSasBuilder.ToSasQueryParameters(GetNewSharedKeyCredentials()) }; // Act AppendBlobClient sasBlobClient = InstrumentClient(new AppendBlobClient(blobUriBuilder.ToUri(), GetOptions())); await sasBlobClient.DeleteAsync(); }
public void BlobUriBuilder_RegularUrl_VersionIdTest() { // Arrange IList <string> snapshotUris = new List <string>() { "https://account.blob.core.windows.net/container/blob?versionid=2011-03-09T01:42:34.9360000Z", "https://account.blob.core.windows.net/container/blob?versionId=2011-03-09T01:42:34.9360000Z", "https://account.blob.core.windows.net/container/blob?VersionId=2011-03-09T01:42:34.9360000Z", "https://account.blob.core.windows.net/container/blob?VERSIONID=2011-03-09T01:42:34.9360000Z", }; foreach (var uriString in snapshotUris) { // Arrange var originalUri = new UriBuilder(uriString); // Act var blobUriBuilder = new BlobUriBuilder(originalUri.Uri); Uri newUri = blobUriBuilder.ToUri(); // Assert Assert.AreEqual("https", blobUriBuilder.Scheme); Assert.AreEqual("account.blob.core.windows.net", blobUriBuilder.Host); Assert.AreEqual("account", blobUriBuilder.AccountName); Assert.AreEqual("container", blobUriBuilder.BlobContainerName); Assert.AreEqual("blob", blobUriBuilder.BlobName); Assert.AreEqual("2011-03-09T01:42:34.9360000Z", blobUriBuilder.VersionId); Assert.IsNull(blobUriBuilder.Sas); Assert.AreEqual("", blobUriBuilder.Query); Assert.AreEqual(443, blobUriBuilder.Port); Assert.IsTrue(string.Equals(originalUri.Uri.AbsoluteUri, newUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase)); } }
public async Task SasBuilderIdentifier() { string accountName = StorageAccountName; string accountKey = StorageAccountKey; string containerName = Randomize("sample-container"); string blobName = Randomize("sample-blob"); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey); // setup blob var container = new BlobContainerClient(ConnectionString, containerName); try { await container.CreateAsync(); await container.GetBlobClient(blobName).UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("hello world"))); // Create one or more stored access policies. List <BlobSignedIdentifier> signedIdentifiers = new List <BlobSignedIdentifier> { new BlobSignedIdentifier { Id = "mysignedidentifier", AccessPolicy = new BlobAccessPolicy { StartsOn = DateTimeOffset.UtcNow.AddHours(-1), ExpiresOn = DateTimeOffset.UtcNow.AddDays(1), Permissions = "rw" } } }; // Set the container's access policy. await container.SetAccessPolicyAsync(permissions : signedIdentifiers); #region Snippet:SampleSnippetsBlobMigration_SasBuilderIdentifier // Create BlobSasBuilder and specify parameters BlobSasBuilder sasBuilder = new BlobSasBuilder() { Identifier = "mysignedidentifier" }; #endregion // Create full, self-authenticating URI to the resource BlobUriBuilder uriBuilder = new BlobUriBuilder(StorageAccountBlobUri) { BlobContainerName = containerName, BlobName = blobName, Sas = sasBuilder.ToSasQueryParameters(sharedKeyCredential) }; Uri sasUri = uriBuilder.ToUri(); // successful download indicates pass await new BlobClient(sasUri).DownloadToAsync(new MemoryStream()); } finally { await container.DeleteIfExistsAsync(); } }
public Uri GetUriOfFile(string folder, string name) { BlobUriBuilder blobUri = new BlobUriBuilder(new Uri(_storageUri)); blobUri.BlobContainerName = _container; blobUri.BlobName = $"{folder}/{name}"; return(blobUri.ToUri()); }
/// <inheritdoc/> public async Task <Uri[]> CopyOutputAssetToOutputContainerAsync(string jobId) { IDictionary <string, string> correlationDataDictionary = await GetCorrelationDataDictionaryAsync(jobId).ConfigureAwait(false); if (correlationDataDictionary == null || !correlationDataDictionary.ContainsKey("outputAssetContainer")) { _log.LogError($"Expected outputAssetContainer in correlationDataDictionary from {jobId}."); throw new Exception($"Expected outputAssetContainer in correlationDataDictionary from {jobId}."); } var outputAssetContainer = correlationDataDictionary["outputAssetContainer"]; var outputAssetContainerUri = new Uri(outputAssetContainer); (string firstOutputAssetId, _) = await _mediaServicesV2RestSharp.GetFirstOutputAssetAsync(jobId).ConfigureAwait(false); (_, Uri outputAssetUri) = await _mediaServicesV2RestSharp.GetAssetNameAndUriAsync(firstOutputAssetId).ConfigureAwait(false); IEnumerable <string> outputAssetFileNames = await _mediaServicesV2RestSharp.GetAssetFilesNames(firstOutputAssetId).ConfigureAwait(false); List <Uri> outputUris = new List <Uri> { }; foreach (var assetFileName in outputAssetFileNames) { try { var sourceUriBuilder = new BlobUriBuilder(outputAssetUri) { BlobName = assetFileName, }; var destUriBuilder = new BlobUriBuilder(outputAssetContainerUri) { BlobName = assetFileName, }; var s = new Stopwatch(); s.Start(); var copyFromUriOperation = await _storageService.BlobCopyAsync(sourceUriBuilder.ToUri(), destUriBuilder.ToUri()).ConfigureAwait(false); var response = await copyFromUriOperation.WaitForCompletionAsync().ConfigureAwait(false); s.Stop(); outputUris.Add(destUriBuilder.ToUri()); DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat; _log.LogInformation($"MediaServicesV2CopyFileCompleted {s.ElapsedMilliseconds.ToString("G", CultureInfo.InvariantCulture)}"); } catch (Exception e) { _log.LogError($"Failed to copy {assetFileName} {outputAssetContainer} for {jobId}"); throw new Exception($"Failed to copy {assetFileName} {outputAssetContainer} for {jobId}", e); } _log.LogInformation($"Copied output asset to {outputAssetContainer} for {jobId}"); } return(outputUris.ToArray()); }
/// <summary> /// Initializes a new instance of the <see cref="AppendBlobClient"/> /// class with an identical <see cref="Uri"/> source but the specified /// <paramref name="snapshot"/> timestamp. /// /// For more information, see <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/creating-a-snapshot-of-a-blob" />. /// </summary> /// <param name="snapshot">The snapshot identifier.</param> /// <returns>A new <see cref="AppendBlobClient"/> instance.</returns> /// <remarks> /// Pass null or empty string to remove the snapshot returning a URL /// to the base blob. /// </remarks> public new AppendBlobClient WithSnapshot(string snapshot) { var builder = new BlobUriBuilder(Uri) { Snapshot = snapshot }; return(new AppendBlobClient(builder.ToUri(), Pipeline, Version, ClientDiagnostics, CustomerProvidedKey, EncryptionScope)); }
/// <summary> /// Creates a new instance of the <see cref="PageBlobClient"/> class /// with an identical <see cref="Uri"/> source but the specified /// <paramref name="snapshot"/> timestamp. /// </summary> /// <param name="snapshot">The snapshot identifier.</param> /// <returns>A new <see cref="PageBlobClient"/> instance.</returns> protected sealed override BlobClient WithSnapshotImpl(string snapshot) { var builder = new BlobUriBuilder(this.Uri) { Snapshot = snapshot }; return(new PageBlobClient(builder.ToUri(), this.Pipeline)); }
/// <summary> /// Initializes a new instance of the <see cref="AppendBlobClient"/> /// class with an identical <see cref="Uri"/> source but the specified /// <paramref name="snapshot"/> timestamp. /// /// For more information, see <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/creating-a-snapshot-of-a-blob" />. /// </summary> /// <param name="snapshot">The snapshot identifier.</param> /// <returns>A new <see cref="AppendBlobClient"/> instance.</returns> /// <remarks> /// Pass null or empty string to remove the snapshot returning a URL /// to the base blob. /// </remarks> public new AppendBlobClient WithSnapshot(string snapshot) { var builder = new BlobUriBuilder(this.Uri) { Snapshot = snapshot }; return(new AppendBlobClient(builder.ToUri(), this.Pipeline)); }
public Uri GetDirectDownloadUri(string name) { BlobUriBuilder uriBuilder = new BlobUriBuilder(this.containerClient.Uri); uriBuilder.BlobContainerName = this.containerClient.Name; uriBuilder.BlobName = name; return(uriBuilder.ToUri()); }
/// <inheritdoc/> public async Task <Uri[]> CopyOutputAssetToOutputContainerAsync(string jobId) { IDictionary <string, string> correlationDataDictionary = await GetCorrelationDataDictionaryAsync(jobId).ConfigureAwait(false); if (correlationDataDictionary == null || !correlationDataDictionary.ContainsKey("outputAssetContainer")) { _log.LogEventObject(LogEventIds.MediaServicesV2CorrelationDataError, jobId); throw new GridwichEncodeCreateJobException($"Expected outputAssetContainer in correlationDataDictionary from {jobId}.", null, null, LogEventIds.MediaServicesV2CorrelationDataError); } var outputAssetContainer = correlationDataDictionary["outputAssetContainer"]; var outputAssetContainerUri = new Uri(outputAssetContainer); (string firstOutputAssetId, _) = await _mediaServicesV2RestWrapper.GetFirstOutputAssetAsync(jobId).ConfigureAwait(false); (_, Uri outputAssetUri) = await _mediaServicesV2RestWrapper.GetAssetNameAndUriAsync(firstOutputAssetId).ConfigureAwait(false); IEnumerable <string> outputAssetFileNames = await _mediaServicesV2RestWrapper.GetAssetFilesNames(firstOutputAssetId).ConfigureAwait(false); List <Uri> outputUris = new List <Uri> { }; foreach (var assetFileName in outputAssetFileNames) { try { var sourceUriBuilder = new BlobUriBuilder(outputAssetUri) { BlobName = assetFileName }; var destUriBuilder = new BlobUriBuilder(outputAssetContainerUri) { BlobName = assetFileName }; var s = new Stopwatch(); s.Start(); var context = StorageClientProviderContext.None; var copyFromUriOperation = await _storageService.BlobCopy(sourceUriBuilder.ToUri(), destUriBuilder.ToUri(), context).ConfigureAwait(false); var response = await copyFromUriOperation.WaitForCompletionAsync().ConfigureAwait(false); s.Stop(); outputUris.Add(destUriBuilder.ToUri()); DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat; _log.LogEventObject(LogEventIds.MediaServicesV2CopyFileCompleted, new { CopyElapsedMilliseconds = s.ElapsedMilliseconds.ToString(dtfi) }); } catch (Exception e) { _log.LogExceptionObject(LogEventIds.MediaServicesV2CopyOutputAssetError, e, new { assetFileName, jobId }); throw new GridwichEncodeCreateJobException($"Could not copy output asset for {jobId}.", null, e, LogEventIds.MediaServicesV2CopyOutputAssetError); } } return(outputUris.ToArray()); }
private Uri GetAccountUri(out string accountName) { var blobUriBuilder = new BlobUriBuilder(Options.AccountUri); blobUriBuilder.Query = null; blobUriBuilder.BlobName = null; blobUriBuilder.BlobContainerName = null; accountName = blobUriBuilder.AccountName; return(blobUriBuilder.ToUri()); }
private Uri GetAccountUri(AzureBlobEgressProviderOptions options, out string accountName) { var blobUriBuilder = new BlobUriBuilder(options.AccountUri); blobUriBuilder.Query = null; blobUriBuilder.BlobName = null; blobUriBuilder.BlobContainerName = null; accountName = blobUriBuilder.AccountName; return(blobUriBuilder.ToUri()); }
public void BlobUriBuilder_RoundTrip() { var service = TestHelper.GetServiceClient_AccountSas(); var blobUriBuilder = new BlobUriBuilder(service.Uri); var blobUri = blobUriBuilder.ToUri(); var expectedUri = WebUtility.UrlDecode(service.Uri.AbsoluteUri); var actualUri = WebUtility.UrlDecode(blobUri.AbsoluteUri); Assert.AreEqual(expectedUri, actualUri, "Flaky test -- potential signature generation issue not properly encoding space and + in the output"); }
private (ServiceRestClient, ContainerRestClient) BuildRestClients() { BlobUriBuilder uriBuilder = new BlobUriBuilder(_uri); uriBuilder.BlobContainerName = null; uriBuilder.BlobName = null; ServiceRestClient serviceRestClient = new ServiceRestClient( clientDiagnostics: _clientDiagnostics, pipeline: _pipeline, url: uriBuilder.ToUri().ToString(), version: _version.ToVersionString()); ContainerRestClient containerRestClient = new ContainerRestClient( clientDiagnostics: _clientDiagnostics, pipeline: _pipeline, url: uriBuilder.ToUri().ToString(), version: _version.ToVersionString()); return(serviceRestClient, containerRestClient); }
public List <ResourceFile> GetResourceFiles( string containerName, string folderName) { List <ResourceFile> fileList = new List <ResourceFile>(); // Create a service level SAS that only allows reading from service // level APIs AccountSasBuilder sas = new AccountSasBuilder { // Allow access to blobs Services = AccountSasServices.Blobs, // Allow access to all service level APIs ResourceTypes = AccountSasResourceTypes.All, // Specify token expiration in hours ExpiresOn = DateTimeOffset.UtcNow.AddHours(SasKeyExpiryTime) }; // Allow all access => Create, Delete, List, Process, Read, Write & Update sas.SetPermissions(AccountSasPermissions.All); // Create a SharedKeyCredential that we can use to sign the SAS token StorageSharedKeyCredential credential = new StorageSharedKeyCredential(AccountName, AccountKey); string storageAccountUri = String.Format("https://{0}.blob.core.windows.net", AccountName); BlobServiceClient blobSvcClient = new BlobServiceClient(new Uri(storageAccountUri), credential); BlobContainerClient containerClient = blobSvcClient.GetBlobContainerClient(containerName); ResourceFile file = null; // folderName is a prefix, does not need to include trailing slash '/' foreach (BlobItem blobItem in containerClient.GetBlobs(Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates.None, folderName)) { BlobUriBuilder blobUri = new BlobUriBuilder(new Uri(storageAccountUri)); blobUri.BlobContainerName = containerName; blobUri.BlobName = blobItem.Name; blobUri.Query = sas.ToSasQueryParameters(credential).ToString(); file = ResourceFile.FromUrl(blobUri.ToUri().ToString(), blobItem.Name); // file.FilePath = folderName; fileList.Add(file); } ; return(fileList); }
/// <summary> /// Initializes a new instance of the type /// </summary> /// <param name="logger">The logger to write debugging and diagnostics information to</param> /// <param name="metricFactory">A factory to create metric containers from</param> /// <param name="storageAccountUri">The Azure Storage account URI</param> /// <param name="containerName">The name of the container to write the checkpoints to</param> /// <param name="credential">The credential provider such as managed identity provider</param> /// <param name="retryOptions">Rety options for the Azure Storage client, or null for default</param> protected BlobStorageCheckpointBase(ILogger logger, IMetricFactory metricFactory, Uri storageAccountUri, string containerName, TokenCredential credential, RetryOptions retryOptions = null) { Guard.NotNullOrWhitespace(nameof(containerName), containerName); Guard.NotNull(nameof(credential), credential); var options = GetOptions(retryOptions); var builder = new BlobUriBuilder(storageAccountUri); builder.BlobContainerName = containerName; Client = new BlobContainerClient(builder.ToUri(), credential, options); ConfigureInstance(logger, metricFactory); }
public void BlobUriBuilder_RoundTrip() { BlobServiceClient service = GetServiceClient_AccountSas(); var blobUriBuilder = new BlobUriBuilder(service.Uri); Uri blobUri = blobUriBuilder.ToUri(); var expectedUri = WebUtility.UrlDecode(service.Uri.AbsoluteUri); var actualUri = WebUtility.UrlDecode(blobUri.AbsoluteUri); Assert.AreEqual(expectedUri, actualUri); }
/// <inheritdoc/> public string GetSasUrlForBlob(Uri blobUri, TimeSpan ttl, StorageClientProviderContext context) { _ = blobUri ?? throw new ArgumentNullException(nameof(blobUri)); _ = context ?? throw new ArgumentNullException(nameof(context)); try { var blobUriBuilder = new BlobUriBuilder(blobUri); // Create a SAS token that's valid for the TimeSpan, plus a back-off start for clock skew. var timeRange = StorageHelpers.CreateTimeRangeForUrl(ttl); BlobSasBuilder sasBuilder = new BlobSasBuilder { BlobContainerName = blobUriBuilder.BlobContainerName, BlobName = blobUriBuilder.BlobName, Resource = "b", // "b" is for blob StartsOn = timeRange.StartTime, ExpiresOn = timeRange.EndTime, }.UnescapeTargetPath(); // Important adjustment(s) for SAS computation sasBuilder.SetPermissions(BlobSasPermissions.Read); // read permissions only for the SAS. var sleeve = _blobBaseClientProvider.GetBlobClientSleeveForUri(blobUri, context); var userDelegation = sleeve.Service.GetUserDelegationKey(sasBuilder.StartsOn, sasBuilder.ExpiresOn)?.Value; if (userDelegation == null) { var msg = $@"Unable to get a user delegation key from the Storage service for blob {blobUri}"; _log.LogEvent(LogEventIds.StorageServiceMissingConnectionString, msg); throw new GridwichStorageServiceException(blobUri, msg, LogEventIds.StorageServiceMissingConnectionString, context.ClientRequestIdAsJObject); } var sasToken = sasBuilder.ToSasQueryParameters(userDelegation, blobUriBuilder.AccountName); blobUriBuilder.Sas = sasToken; // Construct the full URI, including the SAS token. AbsoluteUri (vs. ToString) is to ensure the %HH escaping is used. return(blobUriBuilder.ToUri().AbsoluteUri); } catch (RequestFailedException e) { var msg = $@"Unable to get a user delegation key from the Storage service for blob {blobUri}"; _log.LogEvent(LogEventIds.StorageServiceMissingConnectionString, msg); throw new GridwichStorageServiceException(blobUri, msg, LogEventIds.StorageServiceMissingConnectionString, context.ClientRequestIdAsJObject, e); } catch (Exception e) { _log.LogExceptionObject(LogEventIds.FailedToCreateBlobSasUriInStorageService, e, blobUri); throw new GridwichStorageServiceException(blobUri, "Failed to generate the SAS url.", LogEventIds.FailedToCreateBlobSasUriInStorageService, context.ClientRequestIdAsJObject, e); } }
public void BlobUriBuilder_SasStartExpiryTimeFormats(string startTime, string expiryTime) { // Arrange Uri initialUri = new Uri($"https://account.blob.core.windows.net/container/blob?sv=2020-04-08&st={WebUtility.UrlEncode(startTime)}&se={WebUtility.UrlEncode(expiryTime)}&sr=b&sp=racwd&sig=jQetX8odiJoZ7Yo0X8vWgh%2FMqRv9WE3GU%2Fr%2BLNMK3GU%3D"); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(initialUri); // Act Uri resultUri = blobUriBuilder.ToUri(); // Assert Assert.AreEqual(initialUri, resultUri); Assert.IsTrue(resultUri.PathAndQuery.Contains($"st={WebUtility.UrlEncode(startTime)}")); Assert.IsTrue(resultUri.PathAndQuery.Contains($"se={WebUtility.UrlEncode(expiryTime)}")); }