2020IP / TwentyTwenty.Storage

A cross-cloud storage abstraction
Other
100 stars 26 forks source link

Call CreateIfNotExist only if ncessary #25

Closed imperugo closed 5 years ago

imperugo commented 5 years ago

It seems the method CreateIfNotExist of Azure Storage SDK isn't thread safe. If I run several concurrent threads that are trying to save media into a storage, I get "The specified container already exists" this:

Error] Unable to upload the media from Storage: https://myurl........
TwentyTwenty.Storage.StorageException: Generic provider exception occurred ---> Microsoft.WindowsAzure.Storage.StorageException: The specified container already exists.
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteAsyncInternal[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType accessType, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
   at TwentyTwenty.Storage.Azure.AzureStorageProvider.SaveBlobStreamAsync(String containerName, String blobName, Stream source, BlobProperties properties, Boolean closeStream, Nullable`1 length)
   --- End of inner exception stack trace ---
   at TwentyTwenty.Storage.Azure.AzureStorageProvider.SaveBlobStreamAsync(String containerName, String blobName, Stream source, BlobProperties properties, Boolean closeStream, Nullable`1 length)
   at........

Looking on internet (here a reference but there are tons of articles that talk about this problems) suggest to call it only in case is necessary so I used this code:

if(!await container.ExistsAsync().ConfigureAwait(false))
{
      created = await container.CreateIfNotExistsAsync(security, _requestOptions, _context).ConfigureAwait(false);
}

Hope it helps

ericgreenmix commented 5 years ago

@imperugo thanks for this! Should be available as 2.11.0 once Nuget index's it.