使用 Web Api 将文件上传到 Azure blob
Upload files to Azure blob using Web Api
我需要将文件上传到 Azure blob.I 已尝试如图所示 below.But 它没有 work.Hope 我正在做 wrongly.Earlier 我已经使用了该文件系统存储 images.But 现在我需要将它存储在 Blob 中。
注: blockBlob.UploadFromStream(filestream);//after this point it doesn't work
网络Api方法
[HttpPost]
public async Task<HttpResponseMessage> AddPictures()
{
if (!Request.Content.IsMimeMultipartContent())
{
Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
var newImageName = string.Empty;
var path = System.Web.Hosting.HostingEnvironment.MapPath("~");
var provider = GetMultipartProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var r in provider.FileData)
{
var uploadedFileInfo = new FileInfo(r.LocalFileName);
var originalFileName = GetDeserializedFileName(r);
var extension = Path.GetExtension(originalFileName);
if (extension == null) continue;
var ext = extension.ToLower();
var guid = Guid.NewGuid().ToString();
newImageName = guid + ext;
var storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("pictures", "key"),true);
// Create the blob client.
var blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
var container = blobClient.GetContainerReference("ippictures");
// Retrieve reference to a blob named "myblob".
var blockBlob = container.GetBlockBlobReference(newImageName);
using (var filestream = File.OpenRead(r.LocalFileName))
{
blockBlob.UploadFromStream(filestream);//after this point it doesn't work
}
File.Delete(r.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK, new { newImageName });
}
AngularJS方法
//to add Pictures
vm.addPictures = function ($files, errFiles) {
vm.upload = [];
vm.errFiles = errFiles;
if ($files && $files.length) {
//$files: an array of files selected, each file has name, size, and type
for (var i = 0; i < $files.length; i++) {
var $file = $files[i];
(function (index) {
vm.upload[index] = upload.upload({
url: "/api/Picture/AddPictures",
method: "POST",
data: {},
file: $file
}).progress(function () {
}).success(function (data) {
vm.pictureList.push({
id: vm.pictureList.length + 1,
url: '/common/pictures/' + data.newImageName,
note: '',
isSelected: true,
});
}).error(function () {
});
})(i);
}
}
};
堆栈跟踪
at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1
cmd, IRetryPolicy policy, OperationContext operationContext) in
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line
604 at
Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream
source, Nullable
1 length, AccessCondition accessCondition,
BlobRequestOptions options, OperationContext operationContext) in
c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line
397 at
Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream
source, AccessCondition accessCondition, BlobRequestOptions options,
OperationContext operationContext) in c:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line
295 at
Joshi.IP.WebApi.Controllers.PictureController.d__0.MoveNext()
in D:\my\my.WebApi\WebApi\Controllers\PictureController.cs:line
116
异常信息:
The remote server returned an error: (404) Not Found.
Blob 容器
您的存储帐户名为 ippictures,但容器名为 ip-pictures。在您的代码中,您执行 blobClient.GetContainerReference("ippictures");
而不是 blobClient.GetContainerReference("ip-pictures");
您可以通过检查指定的容器是否存在来为这些情况添加一个安全网:
var container = blobClient.GetContainerReference("ip-pictures");
container.CreateIfNotExists()
我需要将文件上传到 Azure blob.I 已尝试如图所示 below.But 它没有 work.Hope 我正在做 wrongly.Earlier 我已经使用了该文件系统存储 images.But 现在我需要将它存储在 Blob 中。
注: blockBlob.UploadFromStream(filestream);//after this point it doesn't work
网络Api方法
[HttpPost]
public async Task<HttpResponseMessage> AddPictures()
{
if (!Request.Content.IsMimeMultipartContent())
{
Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
var newImageName = string.Empty;
var path = System.Web.Hosting.HostingEnvironment.MapPath("~");
var provider = GetMultipartProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var r in provider.FileData)
{
var uploadedFileInfo = new FileInfo(r.LocalFileName);
var originalFileName = GetDeserializedFileName(r);
var extension = Path.GetExtension(originalFileName);
if (extension == null) continue;
var ext = extension.ToLower();
var guid = Guid.NewGuid().ToString();
newImageName = guid + ext;
var storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("pictures", "key"),true);
// Create the blob client.
var blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
var container = blobClient.GetContainerReference("ippictures");
// Retrieve reference to a blob named "myblob".
var blockBlob = container.GetBlockBlobReference(newImageName);
using (var filestream = File.OpenRead(r.LocalFileName))
{
blockBlob.UploadFromStream(filestream);//after this point it doesn't work
}
File.Delete(r.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK, new { newImageName });
}
AngularJS方法
//to add Pictures
vm.addPictures = function ($files, errFiles) {
vm.upload = [];
vm.errFiles = errFiles;
if ($files && $files.length) {
//$files: an array of files selected, each file has name, size, and type
for (var i = 0; i < $files.length; i++) {
var $file = $files[i];
(function (index) {
vm.upload[index] = upload.upload({
url: "/api/Picture/AddPictures",
method: "POST",
data: {},
file: $file
}).progress(function () {
}).success(function (data) {
vm.pictureList.push({
id: vm.pictureList.length + 1,
url: '/common/pictures/' + data.newImageName,
note: '',
isSelected: true,
});
}).error(function () {
});
})(i);
}
}
};
堆栈跟踪
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604 at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable
1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 397 at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 295 at Joshi.IP.WebApi.Controllers.PictureController.d__0.MoveNext() in D:\my\my.WebApi\WebApi\Controllers\PictureController.cs:line 116
异常信息:
The remote server returned an error: (404) Not Found.
Blob 容器
您的存储帐户名为 ippictures,但容器名为 ip-pictures。在您的代码中,您执行 blobClient.GetContainerReference("ippictures");
而不是 blobClient.GetContainerReference("ip-pictures");
您可以通过检查指定的容器是否存在来为这些情况添加一个安全网:
var container = blobClient.GetContainerReference("ip-pictures");
container.CreateIfNotExists()