简体   繁体   English

通过具有V1运行时的Azure Function将MP3从blob存储流传输到客户端

[英]Stream MP3 from blob storage to client via Azure Function with V1 runtime

I'm attempting to stream files from blob storage through a HTTP-triggered Azure Function so I can gather some metrics about downloads, but I haven't been able to get the actual streaming working correctly. 我试图通过HTTP触发的Azure函数从blob存储流式传输文件,以便可以收集有关下载的一些指标,但是我无法使实际的流式传输正常工作。 Here's what I've managed to get working so far: 到目前为止,这是我设法完成的工作:

using System.Net;
using System.Net.Http.Headers;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string inputBlob, TraceWriter log)
{
    return req.CreateResponse(HttpStatusCode.OK, inputBlob);
}

With function.json looking like this: 使用function.json看起来像这样:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "inputBlob",
      "path": "my-container/sample-file.mp3",
      "connection": "<appsetting>",
      "direction": "in"
    }
  ],
  "disabled": false
}

This almost seems to work, but it doesn't return the correct Content-Type header (it should be audio/mpeg but appears as application/xml; charset=utf-8 ), and for some reason, the Content-Length of the response is almost three times bigger than expected. 这似乎几乎可以正常工作,但是它没有返回正确的Content-Type标头(应为audio/mpeg但应显示为application/xml; charset=utf-8 ),并且由于某种原因,它的Content-Length响应几乎比预期大三倍。

Now I will show you how to download files from Azure blob Storage to your local: 1. make sure you have file in the azure blob storage. 现在,我将向您展示如何将文件从Azure blob存储下载到本地:1.确保文件在azure blob存储中。

在此处输入图片说明

2.Code. 2.代码

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace DownloadFileV1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=bowmanimagestorage02;AccountKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;EndpointSuffix=core.windows.net");
            var myClient = storageAccount.CreateCloudBlobClient();
            var container = myClient.GetContainerReference("images");
            container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
            var blockBlob = container.GetBlockBlobReference("grass.jpg");
            using (var fileStream = System.IO.File.OpenWrite(@"C:\Users\bowmanzh\Downloads\download1.jpg"))
            {
                blockBlob.DownloadToStream(fileStream);
            }
            return req.CreateResponse(HttpStatusCode.BadRequest, "");
        }
    }
}

3.send request. 3.发送请求。

Then you can get the files in the cloud locally. 然后,您可以本地获取云中的文件。 I used the image, but even the video or music files are no problem. 我使用了图像,但是即使是视频或音乐文件也没问题。

在此处输入图片说明 在此处输入图片说明

If you have more issues, please let me know. 如果您还有其他问题,请告诉我。

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

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