简体   繁体   English

如何从c#中的URL将文件直接上传到azure blob?

[英]How to upload a file directly to azure blob from the URL in c#?

I have a url which looks like following "https://XXXXXXXX-functions.azurewebsites.net/api/UploadedZIPFile?ticket=1234&code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" which contains the file.我有一个 url 看起来像“https://XXXXXXXX-functions.azurewebsites.net/api/UploadedZIPFile?ticket=1234&code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”,其中包含该文件。 "UploadedZIPFile" is the file Name. “UploadedZIPFile”是文件名。 In URL I don't have the extension of filename.在 URL 中,我没有文件名的扩展名。 After downloading the file into my system it is showing the extension.将文件下载到我的系统后,它会显示扩展名。 I want to upload the file which came from the URL into Azure blob storage.我想将来自 URL 的文件上传到 Azure blob 存储。 How can I upload the file to azure blob storage from the given URL using c#?如何使用 c# 从给定的 URL 将文件上传到 azure blob 存储? Please Added my code sample below请在下面添加我的代码示例

public async Task<string> automaticFileUpload(string ticketNum, string type, string fileURL) 
        {
            Uri uri = new Uri(fileURL);
            string fileName = System.IO.Path.GetFileName(uri.LocalPath);
            string extension = ".zip";
            string finalFileName = fileName + extension;

            

            var connectionString = "xxxxxxx";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer =
                cloudBlobClient.GetContainerReference("xxxxxxx");
            await cloudBlobContainer.CreateIfNotExistsAsync();
            BlobContainerPermissions permissions = new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            };
            await cloudBlobContainer.SetPermissionsAsync(permissions);
            if (!cloudBlobContainer.GetPermissionsAsync().Equals(BlobContainerPublicAccessType.Off))
            {
                await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Off
                });
            }

            CloudBlockBlob blockblob = cloudBlobContainer.GetBlockBlobReference(finalFileName);
            blockblob.Properties.ContentType = "application/zip";

            using (var client = new HttpClient())
            {
                //get the url 
                var request = new HttpRequestMessage(HttpMethod.Get, fileURL);
                var sendTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                var response = sendTask.Result;

                HttpStatusCode status = response.StatusCode;
                if (status == HttpStatusCode.OK)
                {
                    var httpStream = response.Content.ReadAsStreamAsync().Result;
                    MemoryStream ms = new MemoryStream();
                    httpStream.CopyTo(ms);
                    ms.Position = 0;
                    await blockblob.UploadFromStreamAsync(ms);
                   
                    return blockblob.Uri.AbsoluteUri;
                }
            }

            return null;
 }

As of now I am taking hard coded values for file extension and content type But I need dynamic values截至目前,我正在为文件扩展名和内容类型采用硬编码值但我需要动态值

Thanks in Advance提前致谢

You could get the content-type in the response headers, see:您可以在响应标头中获取content-type ,请参阅:

在此处输入图片说明

And then get file extension from content type in this way .然后以这种方式从内容类型中获取文件extension

There is the code for getting the headers with HttpWebResponse.有使用 HttpWebResponse 获取标头的代码。 For more details, see the document .有关更多详细信息,请参阅文档

// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)
    Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]);
// Releases the resources of the response.
myHttpWebResponse.Close();

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

相关问题 从HTML网页(C#)将文件上传到Azure Blob吗? - Upload file to Azure Blob from HTML webpage (C#)? 如何使用 C# 将文件上传到 Azure Blob 存储? - How to upload file into Azure Blob Storage using C#? 如何将文件上传到 Azure blob 但在 c# 中使用不同的文件名? - How to upload file to Azure blob but with different filename in c#? 如何从 Azure Blob 存储 C# 中的文件 URL 获取容器名称 - How to get container name from file URL in Azure Blob Storage C# 有没有办法直接从C#应用程序将文件写入Azure Blob存储? - Is there any way of writing file to Azure Blob Storage directly from C# application? 使用C#将文件从FTP直接传输到Azure Blob存储 - Transferring files from FTP directly to Azure Blob storage in C# 如何使用 c# 从 Azure 中删除 blob 不可变文件 - How to delete blob immutable file from Azure using c# C#Azure将文件上载到“文件存储服务” - 而不是Blob存储 - C# Azure Upload Files to “File Storage Service” - not Blob Storage 如何使用 Blazor 将大文件上传到 WebAPI (C#) 并将其存储到 Azure Blob 存储中 - How to upload large file using Blazor to WebAPI (C#) and store it into Azure Blob storage 如何使用C#将DataTable上传到Azure Blob存储? - How to upload a DataTable to Azure Blob Storage using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM