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

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

我有一个 url 看起来像“https://XXXXXXXX-functions.azurewebsites.net/api/UploadedZIPFile?ticket=1234&code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”,其中包含该文件。 “UploadedZIPFile”是文件名。在 URL 我没有文件扩展名。将文件下载到我的系统后,它显示了扩展名。我想将来自 URL 的文件上传到 Azure blob 存储中。如何使用 c# 将文件从给定的 URL 上传到 azure blob 存储?请在下面添加我的代码示例

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;
 }

截至目前,我正在为文件扩展名和内容类型采用硬编码值,但我需要动态值

提前致谢

您可以在响应 headers 中得到 content-type,请参阅:

然后从 way 中的内容类型中获取文件 extension

有使用 HttpWebResponse 获取 headers 的代码。有关详细信息,请参阅 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();