在C#中使用Azure.Storage通过多线程上传多个文件

时间:2019-01-30 12:08:32

标签: c# multithreading file-upload azure-storage azure-blob-storage

您好,当我使用WindowsAzure.Storage 2.0.4.0时,我能够在多个线程中上传多个文件。但是我最近将我的库升级到了9.3.3。 现在我在设置多个线程上载文件时遇到错误。请查看我的代码,并告诉我我想念的地方。尽管我已经搜索过设置并行线程,但是它没有像以前那样设置blob的线程。

public void UploadBlobAsync(Microsoft.WindowsAzure.StorageClient.CloudBlob 
blob, string LocalFile)
    {
        Microsoft.WindowsAzure.StorageCredentialsAccountAndKey account = blob.ServiceClient.Credentials as Microsoft.WindowsAzure.StorageCredentialsAccountAndKey;
        ICloudBlob blob2 = new CloudBlockBlob(blob.Attributes.Uri, new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(blob.ServiceClient.Credentials.AccountName, account.Credentials.ExportBase64EncodedKey()));
        UploadBlobAsync(blob2, LocalFile);
    }

public void UploadBlobAsync(ICloudBlob blob, string LocalFile)
    {
        // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
        // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
        // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
        lock (WorkingLock)
        {
            if (!Working)
                Working = true;
            else
                throw new Exception("BlobTransfer already initiated.  Create new BlobTransfer object to initiate a new file transfer.");
        }

        // Attempt to open the file first so that we throw an exception before getting into the async work
        using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { }

        // Create an async op in order to raise the events back to the client on the correct thread.
        asyncOp = AsyncOperationManager.CreateOperation(blob);

        TransferType = TransferTypeEnum.Upload;
        m_Blob = blob;
        m_FileName = LocalFile;

        var file = new FileInfo(m_FileName);
        long fileSize = file.Length;

        FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        ProgressStream pstream = new ProgressStream(fs);
        pstream.ProgressChanged += pstream_ProgressChanged;
        pstream.SetLength(fileSize);

        m_Blob.ServiceClient.ParallelOperationThreadCount = 10; //This Line is giving an error that is does not contain the definition.
        m_Blob.StreamWriteSizeInBytes = GetBlockSize(fileSize);
        asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
    }

m_Blob.ServiceClient.ParallelOperationThreadCount = 10;给出的错误是它不包含定义。当我试图找到解决办法,但找不到。我在Microsoft论坛上找到了代码,但并没有太大帮助。

1 个答案:

答案 0 :(得分:1)

更新后的代码以多线程方式上传了azure blob,存储了多个文件,这是更新后的代码片段,可以集成到我之前的代码中。

//Replace 

m_Blob.ServiceClient.ParallelOperationThreadCount = 10

//with

    BlobRequestOptions options = new BlobRequestOptions
        {
            ParallelOperationThreadCount = 8,
            DisableContentMD5Validation = true,
            StoreBlobContentMD5 = false
        };
//Replace 
 asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));

//with
asyncresult = m_Blob.BeginUploadFromStream(pstream,null,options,null,BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));