在发布的应用程序中上传到Blob存储时出现IOException

Rec*_*iwe 5 c# azure azure-blob-storage

我开发了一个简单的应用程序,只需将文件从文件夹上传到Azure Blob存储,从VS运行时我运行良好,但是在发布的应用程序中,偶尔会出现此错误:

ved System.IO .__ Error.WinIOError(Int32 errorCode,字符串,可能是FullPath)ved System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)ved System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问权限)ved Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlobBlob.UploadFromFile(字符串路径,FileMode模式,AccessCondition,accessCondition,BlobRequestOptions选项,OperationsContext operationContext)ved Program.MainWindow.Process(对象发送者,NotifyCollectionChangedEventArgs e)

我的上传代码如下:

private void Process(object sender, NotifyCollectionChangedEventArgs e)
{
    if (paths.Count > 0){
        var currentPath = paths.Dequeue();
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UserSettings.Instance.getConnectionString());
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(UserSettings.Instance.getContainer());
        CloudBlockBlob b = blobContainer.GetBlockBlobReference(System.IO.Path.GetFileName(currentPath));
        try
        {
           b.UploadFromFile(currentPath, FileMode.Open);
        }
        catch (StorageException s)
        {
            throw new System.InvalidOperationException("Could not connect to the specified storage account. Please check the configuration.");
        }
        catch (IOException exc)
        {
            throw new System.InvalidOperationException(exc.StackTrace);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

捕获中的IOException有时会被击中,您知道如何解决此问题吗?

如果查看文档,我会被告知,如果发生存储服务错误,则会发生异常。任何想法如何进一步调查?

https://docs.microsoft.com/zh-cn/java/api/com.microsoft.azure.storage.blob._cloud_blob.uploadfromfile?view=azure-java-legacy#com_microsoft_azure_storage_blob__cloud_blob_uploadFromFile_final_String_

小智 1

我发现只有当我将文件复制到受监视的文件夹中时才会出现错误,如果我拖动它们,它就可以正常工作吗?

看起来您的应用程序正在尝试读取仍在写入磁盘(不完整)或被其他进程锁定的本地文件。拖动文件在某种程度上是“原子”操作(即非常快),因此大大减少了出现此错误的机会。

尝试实现此答案中的方法来测试文件在调用之前是否未锁定UploadFromFile()。现在,根据您的代码逻辑,如果文件被锁定,您将需要实现某种形式的“重试”。这是一个例子:

    protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        //file is not locked
        return false;
    }

    private void Process(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (paths.Count > 0)
        {
            var currentPath = paths.Dequeue();
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UserSettings.Instance.getConnectionString());
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(UserSettings.Instance.getContainer());
            CloudBlockBlob b = blobContainer.GetBlockBlobReference(System.IO.Path.GetFileName(currentPath));
            try
            {
                FileInfo fi = new FileInfo(currentPath);
                while (IsFileLocked(fi))
                    Thread.Sleep(5000); // Wait 5 seconds before checking again
                b.UploadFromFile(currentPath, FileMode.Open);
            }
            catch (StorageException s)
            {
                throw new System.InvalidOperationException("Could not connect to the specified storage account. Please check the configuration.");
            }
            catch (IOException exc)
            {
                throw new System.InvalidOperationException(exc.StackTrace);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

请注意,这决不能保证文件不会在IsFileLocked()调用和b.UploadFromFile()调用之间被另一个进程再次锁定。