我正在尝试编写测试WCF服务,该服务将图像文件写入Azure云端存储:
以下是我写的代码:
var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("mydrives");
container.CreateIfNotExist();
CloudPageBlob pageBlob = container.GetPageBlobReference("myvhd");
CloudDrive drive = new CloudDrive(pageBlob.Uri, storageAccount.Credentials);
try
{
drive.Create(100);
driveLetter = drive.Mount(0, DriveMountOptions.None);
var fileExtension = Path.GetExtension(file.FileName);
var fileName = string.Format("{0:10}_{1}{2}", DateTime.Now.Ticks, Guid.NewGuid(), fileExtension);
File.WriteAllBytes(driveLetter + "\\" + fileName, file.FileStream);
}
catch (CloudDriveException e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
file.FileStream是我从Silverlight客户端获取的字节数组。
当我在本地运行它时,此代码非常有效。我甚至可以看到使用存储模拟器查看器上传的驱动器blob和图像。
但是一旦我将它发布到云端并运行它,我就会遇到以下异常:
<InnerException i:nil="true"/><Message>Access to the path 'd:\1634586886770888071_aa98357b-888d-4dde-a231-1ca5d5c73b49.jpeg' is denied.</Message><StackTrace>
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.File.WriteAllBytes(String path, Byte[] bytes)
at ProcessService.FileService.UploadDCMToDrive(FileToTransfer file) in C:\Users\Vinod\Desktop\TestAzure_Vinod\ProcessService\FileService.svc.cs:line 68
at SyncInvokeUploadDCMToDrive(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace><Type>System.UnauthorizedAccessException</Type>
我尝试使用提升的权限启动应用程序(根据this)但我仍然得到相同的错误。有人可以对此有所了解吗?
更新:我使用以下代码创建了一个文件夹:
DirectoryInfo di = new DirectoryInfo(driveLetter + "\\images");
if (!di.Exists)
di.Create();
现在我收到了这个错误:
Could not find a part of the path 'd:\images\test.jpeg
真的很奇怪: - (