如何解决Azure在上载文件时,引发异常“指定的Blob不存在”
我通过.net核心开发了一个应用程序。我将Azure存储用于文件。但是有时代码会产生期望:指定的blob不存在。
这是我开始文件上传过程的方式:
entity = FileUploadBusinessRule.UploadFile(entity,companyInformationEntityRequestDto.BusinessLicenseBase64,"BusinessLicenseUrl");
entity = FileUploadBusinessRule.UploadFile(entity,companyInformationEntityRequestDto.CompanyLogoBase64,"CompanyLogoUrl");
FileUploadBusinessRule.UploadFile:
public static class FileUploadBusinessRule
{
public static T UploadFile<T>(T entity,string file,string propName)
{
IStorageService _storageService = DependencyContainerHelper.Container.Resolve<IStorageService>();
Type t = entity.GetType();
var propertyValue = t?.GetProperty(propName)?.GetValue(entity)?.ToString();
if (!file.IsNullOrEmpty())
{
if (file.Contains("http://") || file.Contains("https://"))
return entity;
if (!propertyValue.IsNullOrEmpty())
_storageService.DeleteBlobData(propertyValue);
t?.GetProperty(propName)?.SetValue(entity,_storageService.UploadFileByBase64String(file));
}
else
{
if (!propertyValue.IsNullOrEmpty())
_storageService.DeleteBlobData(propertyValue);
}
return entity;
}
}
_storageService.UploadFileByBase64String:
public string UploadFileByBase64String(string strBase64)
{
var byteArray = strBase64.Base64ToByteArray();
var mimeType = strBase64.GetMIMETypeFromBase64();
var fileName = Guid.NewGuid().ToString() + "." + strBase64.GetExtensionFromBase64();
return this.UploadFile(fileName,byteArray,mimeType);
}
上传文件:
public string UploadFile(string strFileName,byte[] fileData,string fileMimeType)
{
try
{
var _task = Task.Run(() => this.UploadFileToBlobAsync(strFileName,fileData,fileMimeType));
_task.Wait();
string fileUrl = _task.Result;
return fileUrl;
}
catch (Exception ex)
{
throw (ex);
}
}
UploadFileToBlobAsync:
private async Task<string> UploadFileToBlobAsync(string strFileName,string fileMimeType)
{
try
{
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(accessKey);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
string strContainerName = "files";
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);
string fileName = this.GenerateFileName(strFileName);
if (await cloudBlobContainer.CreateIfNotExistsAsync())
{
await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
}
if (fileName != null && fileData != null)
{
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
cloudBlockBlob.Properties.ContentType = fileMimeType;
await cloudBlockBlob.UploadFromByteArrayAsync(fileData,fileData.Length);
return cloudBlockBlob.Uri.AbsoluteUri;
}
return "";
}
catch (Exception ex)
{
throw ex;
}
}
当代码执行第一行(实体= FileUploadBusinessRul ... blabla)时,应用程序可以成功上传文件。但是,当执行第二行时,它将在此行中引发异常:
if (await cloudBlobContainer.CreateIfNotExistsAsync())
我尝试了相同的文件,但是无论如何它都会引发异常。此外,即使我包装了try-catch块,应用程序也会停止运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。