简体   繁体   English

将文件上载到Azure Blob存储

[英]Upload a file to Azure Blob Storage

I want to upload a file to Azure blob storage asynchronously. 我想异步上传文件到Azure blob存储。 I have tried the way suggested in the official sdk: 我试过官方sdk中建议的方式:

This is how I get the container: 这就是我获取容器的方式:

public static class BlobHelper
{
    public static CloudBlobContainer GetBlobContainer()
    {
        // Pull these from config
        var blobStorageConnectionString = ConfigurationManager.AppSettings["BlobStorageConnectionString"];
        var blobStorageContainerName = ConfigurationManager.AppSettings["BlobStorageContainerName"];

        // Create blob client and return reference to the container
        var blobStorageAccount = CloudStorageAccount.Parse(blobStorageConnectionString);
        var blobClient = blobStorageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference(blobStorageContainerName);
        container.CreateIfNotExists();
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        return container;
    }
}

And this is how i try to upload the file: 这就是我尝试上传文件的方式:

var documentName = Guid.NewGuid().ToString();

CloudBlobContainer container = BlobHelper.GetBlobContainer();

CloudBlockBlob blockBlob = container.GetBlockBlobReference(documentName);

public class FilesService
{
    public async Task<string> UploadFiles(HttpContent httpContent)
    {

        var documentName = Guid.NewGuid().ToString();

        CloudBlobContainer container = BlobHelper.GetBlobContainer();

        CloudBlockBlob blockBlob = container.GetBlockBlobReference(documentName);

        using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
        {
            await blockBlob.UploadFromStreamAsync(fileStream);
        }

        return blockBlob.Uri.ToString();
    }
}

The problem is that I do not know how to get the path to my file (it is uploaded by the user). 问题是我不知道如何获取我的文件的路径(它由用户上传)。

When I try this: 当我尝试这个:

var rootpath =  HttpContext.Current.Server.MapPath("~/App_Data");

var streamProvider = new MultipartFileStreamProvider(rootpath);

await httpContent.ReadAsMultipartAsync(streamProvider);
foreach (var file in streamProvider.FileData)
{
    var localName = file.LocalFileName;
    using (var fileStream = System.IO.File.OpenRead(file.LocalFileName))
    {
        await blockBlob.UploadFromStreamAsync(fileStream);
    }
}

And when I try a post request. 当我尝试发布请求时。 The request just crashes and does not return anything (even an exception); 请求崩溃并且不返回任何内容(甚至是异常);

Solution: 解:

The issue was resolved in the following way. 该问题以下列方式得到解决。 I used a service method in order to be able to upload a collection of files. 我使用了一种服务方法,以便能够上传一组文件。

用于上载blob集合的服务方法

In the BlobHelper class I save the needed information about the container and then instantiate it, it is a static class. BlobHelper类中,我保存了有关容器的所需信息,然后实例化它,它是一个static类。 Using a collection makes it possible to upload a multiple files as a part of the same stream. 使用集合可以将多个文件作为同一个流的一部分上载。

I think you are trying to get the path to the file that is being uploaded to the Blob Storage using standard ASP.NET methods and local context. 我认为您正在尝试使用标准ASP.NET方法和本地上下文获取正在上载到Blob存储的文件的路径。 Files uploaded to the blob will not be accessible that way. 上传到blob的文件无法以这种方式访问​​。

Seems like you upload your blob properly. 好像你正确上传你的blob。 Now, if your file uploaded successfully, your method should return blockBlob.Uri.ToString(), which is the link to your file - you may store it somewhere in the database or anywhere else. 现在,如果您的文件成功上传,您的方法应返回blockBlob.Uri.ToString(),它是您文件的链接 - 您可以将其存储在数据库中的某个位置或其他任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM