// Test that the credentials are valid and classify the account.Type as one of StorageAccountTypes private static async Task ValidateCredentialsAsyncCore(IStorageAccount account, CancellationToken cancellationToken) { // Verify the credentials are correct. // Have to actually ping a storage operation. IStorageBlobClient client = account.CreateBlobClient(); try { // This can hang for a long time if the account name is wrong. // If will fail fast if the password is incorrect. await client.GetServicePropertiesAsync(cancellationToken); } catch (OperationCanceledException) { throw; } catch (Exception e) { var storageException = e as StorageException; var isDevStoreAccount = GetIsDevStoreAccountFromCloudStorageAccount(account.SdkObject); if (storageException?.RequestInformation?.HttpStatusCode == 400 && storageException?.RequestInformation?.ExtendedErrorInformation?.ErrorCode == "InvalidQueryParameterValue") { // Premium storage accounts do not support the GetServicePropertiesAsync call, and respond with a 400 'InvalidQueryParameterValue'. // If we see this error response classify the account as a premium account account.Type = StorageAccountType.Premium; return; } else if (isDevStoreAccount) { // If using the storage emulator, it might not be running throw new InvalidOperationException(Constants.CheckAzureStorageEmulatorMessage, e); } else { // If not a recognized error, the credentials are invalid string message = String.Format(CultureInfo.CurrentCulture, "Invalid storage account '{0}'. Please make sure your credentials are correct.", account.Credentials.AccountName); throw new InvalidOperationException(message, e); } } IStorageQueueClient queueClient = account.CreateQueueClient(); IStorageQueue queue = queueClient.GetQueueReference("name"); try { await queue.ExistsAsync(cancellationToken); } catch (StorageException exception) when(IsBlobOnlyStorageException(exception)) { account.Type = StorageAccountType.BlobOnly; } }
private async Task ValidateCredentialsAsyncCore(IStorageAccount account, bool isPrimaryAccount, CancellationToken cancellationToken) { // Verify the credentials are correct. // Have to actually ping a storage operation. IStorageBlobClient client = account.CreateBlobClient(); try { // This can hang for a long time if the account name is wrong. // If will fail fast if the password is incorrect. await client.GetServicePropertiesAsync(cancellationToken); } catch (OperationCanceledException) { throw; } catch { string message = String.Format(CultureInfo.CurrentCulture, "Invalid storage account '{0}'. Please make sure your credentials are correct.", account.Credentials.AccountName); throw new InvalidOperationException(message); } if (isPrimaryAccount) { // Primary storage accounts require Queues IStorageQueueClient queueClient = account.CreateQueueClient(); IStorageQueue queue = queueClient.GetQueueReference("name"); try { await queue.ExistsAsync(cancellationToken); _primaryCredentials = account.Credentials; } catch (OperationCanceledException) { throw; } catch (StorageException exception) { WebException webException = exception.GetBaseException() as WebException; if (webException != null && webException.Status == WebExceptionStatus.NameResolutionFailure) { string message = String.Format(CultureInfo.CurrentCulture, "Invalid storage account '{0}'. Primary storage accounts must be general " + "purpose accounts and not restricted blob storage accounts.", account.Credentials.AccountName); throw new InvalidOperationException(message); } throw; } } }
public static async Task EnableLoggingAsync(IStorageBlobClient blobClient, CancellationToken cancellationToken) { ServiceProperties serviceProperties = await blobClient.GetServicePropertiesAsync(cancellationToken); // Merge write onto it. LoggingProperties loggingProperties = serviceProperties.Logging; if (loggingProperties.LoggingOperations == LoggingOperations.None) { // First activating. Be sure to set a retention policy if there isn't one. loggingProperties.RetentionDays = 7; loggingProperties.LoggingOperations |= LoggingOperations.Write; // Leave metrics untouched await blobClient.SetServicePropertiesAsync(serviceProperties, cancellationToken); } }
private static async Task ValidateCredentialsAsyncCore(IStorageAccount account, CancellationToken cancellationToken) { // Verify the credentials are correct. // Have to actually ping a storage operation. IStorageBlobClient client = account.CreateBlobClient(); try { // This can hang for a long time if the account name is wrong. // If will fail fast if the password is incorrect. await client.GetServicePropertiesAsync(cancellationToken); } catch (OperationCanceledException) { throw; } catch { string message = String.Format(CultureInfo.CurrentCulture, "The account credentials for '{0}' are incorrect.", account.Credentials.AccountName); throw new InvalidOperationException(message); } }
// Test that the credentials are valid and classify the account.Type as one of StorageAccountTypes private static async Task ValidateCredentialsAsyncCore(IStorageAccount account, CancellationToken cancellationToken) { // Verify the credentials are correct. // Have to actually ping a storage operation. IStorageBlobClient client = account.CreateBlobClient(); try { // This can hang for a long time if the account name is wrong. // If will fail fast if the password is incorrect. await client.GetServicePropertiesAsync(cancellationToken); } catch (OperationCanceledException) { throw; } catch (Exception e) { var storageException = e as StorageException; if (storageException?.RequestInformation?.HttpStatusCode == 400 && storageException?.RequestInformation?.ExtendedErrorInformation?.ErrorCode == "InvalidQueryParameterValue") { // Premium storage accounts do not support the GetServicePropertiesAsync call, and respond with a 400 'InvalidQueryParameterValue'. // If we see this error response classify the account as a premium account account.Type = StorageAccountType.Premium; return; } else { // If not a recognized error, the credentials are invalid string message = String.Format(CultureInfo.CurrentCulture, "Invalid storage account '{0}'. Please make sure your credentials are correct.", account.Credentials.AccountName); throw new InvalidOperationException(message); } } IStorageQueueClient queueClient = account.CreateQueueClient(); IStorageQueue queue = queueClient.GetQueueReference("name"); try { await queue.ExistsAsync(cancellationToken); } catch (OperationCanceledException) { throw; } catch (StorageException exception) { WebException webException = exception.GetBaseException() as WebException; if (webException?.Status == WebExceptionStatus.NameResolutionFailure) { // Blob-only storage accounts do not support services other than Blob. // If we see a name resolution failure on the queue endpoint classify as a blob-only account account.Type = StorageAccountType.BlobOnly; return; } throw; } }