简体   繁体   English

上传文件而不将内容复制到根文件夹 .NET Core 2.2

[英]Uploading file without copying content to root folder .NET Core 2.2

I'm playing with Azure Blob Storage and I'm wondering if I can optimize this code a bit.我正在使用 Azure Blob 存储,我想知道是否可以稍微优化此代码。

As you can see I'm creating folder and then uploading content to that folder before sending to Azure Storage and upon competition I'm deleting that folder it seems a bit redundant to be fair.正如您所看到的,我正在创建文件夹,然后在发送到 Azure 存储之前将内容上传到该文件夹​​,并且在竞争时我删除了该文件夹,公平地说,这似乎有点多余。

Now, I'm wondering if I can skip this few steps and just upload stream to azure without copying first to root folder?现在,我想知道是否可以跳过这几个步骤,直接将流上传到 azure 而无需先复制到根文件夹?

Here is the code:这是代码:

   public async Task UploadBlobFile(IFormFile file, BlobMetadata metadata,  string containerName)
    {
        var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);

        BlobHttpHeaders headers = new BlobHttpHeaders
        {
            ContentType = "application/pdf",
            ContentLanguage = "hr-HR",
        };

        if (file.Length > 0)
        {
            try
            {
                var rootFolder = Path.Combine(_hostingEnvironment.WebRootPath, "upload");

                if (!Directory.Exists(rootFolder))
                {
                    Directory.CreateDirectory(rootFolder);
                }

                // create folder if doesnt exists
                var filePath = Path.Combine(rootFolder, file.FileName);
                using (var stream = new FileStream(filePath, FileMode.Create))
                {

                    await file.CopyToAsync(stream);

                    // set cursor to the beginning of the stream.
                    stream.Position = 0;

                    var metadataProperties = new Dictionary<string, string>
                    {
                        { MetadataValues.Id, metadata.Id },
                        { MetadataValues.Name, metadata.Name },
                        { MetadataValues.UniqueName, metadata.UniqueName }
                    };

                    var blobClient = containerClient.GetBlobClient(file.FileName);

                    await blobClient.UploadAsync(stream, headers, metadataProperties);

                }

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

    }

As fellow Bradley Uffner and pinkfloydx33 pointed out, code above can be optimized like this正如Bradley Uffnerpinkfloydx33 所指出的,上面的代码可以这样优化

public async Task UploadBlobFile(IFormFile file, BlobMetadata metadata,  string containerName)
{
    var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
    BlobHttpHeaders headers = new BlobHttpHeaders
    {
        ContentType = "application/pdf",
        ContentLanguage = "hr-HR",
    };

    if (file.Length > 0)
    {
        using (var stream = file.OpenReadStream())
        {
            // set cursor to the beginning of the stream.
            stream.Position = 0;
            var metadataProperties = new Dictionary<string, string>
            {
                { MetadataValues.Id, metadata.Id },
                { MetadataValues.Name, metadata.Name },
                { MetadataValues.UniqueName, metadata.UniqueName }
            };

            var blobClient = containerClient.GetBlobClient(file.FileName);
            await blobClient.UploadAsync(stream, headers, metadataProperties);

        }
    }
}

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

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