我创建了一个简单的.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);
中随机得到以下异常。
“无法在指定时间内验证副本来源。”
完成上述异常的堆栈跟踪:
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.d__4
中的DownloadFilesWebAPI.Controllers.DownloadFilesController.d__9.MoveNext()中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。d.MoveNext() ---从之前引发异常的位置开始的堆栈结束跟踪--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在E:\ XXX \ XXX \ XXX \ DownloadFilesWebAPI \ Controllers \ DownloadFilesController.cs:line 98
如何解决此问题?