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

问题描述 投票:0回答:1

你好,我能在多线程上传多个文件时,我使用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;是给它不包含定义错误。当我试着找周围的工作,但不能。我发现那位在微软论坛的代码,但它并没有多大帮助。

c# multithreading file-upload azure-storage azure-blob-storage
1个回答
1
投票

在这里多线程的方式上传蔚蓝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));
© www.soinside.com 2019 - 2024. All rights reserved.