Salman
Salman

Reputation: 19

Upload multiple files by Multi threading Using Azure.Storage in C#

Hello i was able to upload multiple files in multiple threads when i was using WindowsAzure.Storage 2.0.4.0. but i have recently upgraded my library to 9.3.3. Now i am facing error in setting my multiple threads to upload my file. Please have a look at my code and tell me that where i am missing. Although i have searched to set the parallel threads but its not setting the threads of the blob as it was setting before.

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; is giving the error that it does not contain the definition. As i tried to find the work around but couldn't. I fount the code on Microsoft forum but it didn't help much.

Upvotes: 0

Views: 2762

Answers (1)

Salman
Salman

Reputation: 19

Updated code of uploading azure blob storage multiple files in multi-threaded way here is the snippet of updated code which can be integrated in my previous code.

//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));

Upvotes: 1

Related Questions