Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从c#中的URL将文件直接上载到azure blob?_C#_Asp.net_Azure Blob Storage_Webapi - Fatal编程技术网

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

如何从c#中的URL将文件直接上载到azure blob?,c#,asp.net,azure-blob-storage,webapi,C#,Asp.net,Azure Blob Storage,Webapi,我有一个如下所示的url“https://XXXXXXXX-functions.azurewebsites.net/api/UploadedZIPFile?ticket=1234&code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX“其中包含该文件。“UploadedZIPFile”是文件名。在URL中,我没有文件名的扩展名。将文件下载到我的系统后,它会显示扩展名。我想将来自URL的文件上载到Azure blob存储中。如何使用c#从给定URL将文件上载到azure

我有一个如下所示的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;
 }
公共异步任务自动文件上载(字符串ticketNum、字符串类型、字符串fileURL)
{
Uri=新的Uri(fileURL);
字符串fileName=System.IO.Path.GetFileName(uri.LocalPath);
字符串扩展名=“.zip”;
字符串finalFileName=文件名+扩展名;
var connectionString=“xxxxxxx”;
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(connectionString);
CloudBlobClient CloudBlobClient=storageAccount.CreateCloudBlobClient();
CloudBlobContainer CloudBlobContainer=
cloudBlobClient.GetContainerReference(“xxxxxxx”);
等待cloudBlobContainer.CreateIfNotExistsAsync();
BlobContainerPermissions权限=新建BlobContainerPermissions
{
PublicAccess=BlobContainerPublicAccessType.Blob
};
等待cloudBlobContainer.SetPermissionsAsync(权限);
如果(!cloudBlobContainer.GetPermissionsAsync().Equals(BlobContainerPublicAccessType.Off))
{
等待cloudBlobContainer.SetPermissionsAsync(新BlobContainerPermissions
{
PublicAccess=BlobContainerPublicAccessType.Off
});
}
CloudBlockBlob blockblob=cloudBlobContainer.getblockblobbreference(finalFileName);
blockblob.Properties.ContentType=“应用程序/zip”;
使用(var client=new HttpClient())
{
//获取url
var request=newhttprequestmessage(HttpMethod.Get,fileURL);
var sendTask=client.sendsync(请求,HttpCompletionOption.ResponseHeadersRead);
var response=sendTask.Result;
HttpStatusCode状态=response.StatusCode;
if(status==HttpStatusCode.OK)
{
var httpStream=response.Content.ReadAsStreamAsync().Result;
MemoryStream ms=新的MemoryStream();
httpStream.CopyTo(ms);
ms.Position=0;
等待blockblob.UploadFromStreamAsync(毫秒);
返回blockblob.Uri.AbsoluteUri;
}
}
返回null;
}
到目前为止,我正在为文件扩展名和内容类型采用硬编码值,但我需要动态值


提前感谢

您可以在响应标题中获得
内容类型
,请参阅:

然后从中的内容类型获取文件
扩展名

下面是使用HttpWebResponse获取标题的代码。有关更多详细信息,请参阅

//为指定的URL创建HttpWebRequest。
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
//发送HttpWebRequest并等待响应。
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
//显示从URI接收的响应中存在的所有标头。
Console.WriteLine(“\r\n在响应中接收到以下标题:”);
//显示每个标题及其与响应关联的键。
对于(int i=0;i
您尝试了什么?如果您对代码有特定问题,我们可以帮助您。如果您已经尝试了一些东西,请分享您的尝试。不幸的是,我添加了我的代码样本。你是在问如何检测上传文件的扩展名和内容类型吗?当运行HTTP GET请求将该文件下载到程序中时,应该能够从HTTP响应头中获取内容类型。因此,您可以派生一个合适的扩展名(如果带有扩展名的文件名未包含在响应标题中)。这对我帮助很大
// 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();