【发布时间】:2019-06-23 17:13:04
【问题描述】:
您好,当我使用 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 论坛上找到了代码,但没有多大帮助。
【问题讨论】:
-
嗯,这是您的新代码还是旧代码?你看过官方文档吗:docs.microsoft.com/en-us/azure/storage/blobs/…?
-
这是我使用库 2.0.4.0 时的旧代码。是的,我看过它,但没有多大帮助。
-
你能发布新代码吗?你说它没有帮助是什么意思? SDK 从第 2 版到第 9 版发生了很大变化,因此您不能期望重复使用相同的代码......
-
Thomas 我终于在这里破解了它是我用来使我的旧代码工作的代码的更新的 sn-p。这是新的 sn-p,我们可以使用它以多线程方式上传多个文件。将:m_Blob.ServiceClient.ParallelOperationThreadCount = 10; 替换为:BlobRequestOptions options = new BlobRequestOptions { ParallelOperationThreadCount = 8, DisableContentMD5Validation = true, StoreBlobContentMD5 = false };
-
很好,你应该发布自己的答案然后:-)
标签: c# multithreading file-upload azure-storage azure-blob-storage