我创建了简单的 .net 核心 web api 应用程序,其中添加了一个新的 API 空控制器,并添加了用于从 azure 文件共享获取图像/文件并上传到 azure blob 存储的代码。
这是我在控制器中编写的代码:
// GET: api/UploadFiletoBlob
[HttpGet]
public async Task<string> UploadFiletoBlob()
{
// Retrieve the connection string for use with the application. The storage connection string is stored
// in an environment variable on the machine running the application called storageconnectionstring.
// If the environment variable is created after the application is launched in a console or with Visual
// Studio, the shell needs to be closed and reloaded to take the environment variable into account.
//string storageConnectionString = Environment.GetEnvironmentVariable("Storageconnectionstring");
// Check whether the connection string can be parsed.
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
try
{
// Create a CloudFileClient object for credentialed access to Azure Files.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
// Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference(fileshareName);
// Ensure that the share exists.
if (await share.ExistsAsync())
{
// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
// Get a reference to the directory we created previously.
CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(directoryName);
// Ensure that the directory exists.
if (await sampleDir.ExistsAsync())
{
// Get a reference to the file we created previously.
sourceFile = sampleDir.GetFileReference(sourceFileName);
// Ensure that the file exists.
if (await sourceFile.ExistsAsync())
{
// Get a reference to the blob to which the file will be uploaded.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Ensure that the container exists.
if (await container.ExistsAsync())
{
CloudBlockBlob destBlob = container.GetBlockBlobReference(sourceFile.Name);
// Create a SAS for the file that's valid for 24 hours.
// Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
// to authorize access to the source object, even if you are copying within the same
// storage account.
string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
// Only read permissions are required for the source file.
Permissions = SharedAccessFilePermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});
// Construct the URI to the source file, including the SAS token.
Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);
// Upload the file to the blob.
await destBlob.StartCopyAsync(fileSasUri);
// Write the contents of the file to the output window.
Debug.WriteLine("Successfully uploaded the file into blob: {0}", sourceFile.Name);
}
}
}
}
}
catch (StorageException ex)
{
Console.WriteLine("Error returned from the service: {0}", ex.Message);
return "Failed to upload the file into storage blob";
}
return $"Successfully uploaded the file into storage blob: {sourceFile.Name}";
}
else
{
Debug.WriteLine(
"A connection string has not been defined in the system environment variables. " +
"Add a environment variable named 'storageconnectionstring' with your storage " +
"connection string as a value.");
return "A connection string has not been defined in the system environment variables. " +
"Add a environment variable named 'storageconnectionstring' with your storage " +
"connection string as a value.";
}
}
但是当运行我的应用程序时,我在这行代码中随机得到以下异常await destBlob.StartCopyAsync(fileSasUri);
。
“无法在指定时间内验证复制来源。”</p>
上述异常的完整堆栈跟踪:
在 Microsoft.WindowsAzure.Storage.Core.Executor.Executor.d__4
1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在 Microsoft.WindowsAzure.Storage.Blob.CloudBlob.<>c__DisplayClass111_0.<b__0>d.MoveNext() --- 堆栈跟踪结束以前抛出异常的位置 --- 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() E:\XXX\XXX\XXX\DownloadFilesWebAPI\Controllers\DownloadFilesController.cs:line 98 中的 DownloadFilesWebAPI.Controllers.DownloadFilesController.d__9.MoveNext()
如何解决这个问题?