简体   繁体   English

部署在Azure中但无法通过VS仿真器工作时,文件不会复制到流中

[英]File not copying to stream when deployed in Azure, yet working through VS emulator

My program takes a MP4 file uploads it to a blob then trims it down to 10 seconds and re-uploads it to the blob. 我的程序获取一个MP4文件,将其上传到Blob,然后将其修剪到10秒,然后重新上传到Blob。 I currently have this working when running it through the azure emulator in VS however, when I deploy it to the cloud the output files have 0 bytes. 通过VS中的azure模拟器运行它时,我目前可以正常工作,但是,当我将其部署到云中时,输出文件为0字节。

Trimmer: 微调:

public static void Trimmer(string localStoragePath, Stream output )
{
    string path2= localStoragePath.Remove(localStoragePath.Length-4 )+ "_trimmed.mp4";
    string ffmpeg = "ffmpeg.exe";
    bool success = false;
    string ExeArguments;

    try
    {

        Process proc;
        proc = new Process();
        proc.StartInfo.FileName = ffmpeg;
        ExeArguments = @" -t 10 -i " + localStoragePath + " -map_metadata 0 -acodec copy " 
                                     + path2 + " -y";
        //ExeArguments = @"–t 10 -i -acodec" + localStoragePath + path2 ;
        Trace.TraceInformation(ExeArguments);
        proc.StartInfo.Arguments = ExeArguments;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.ErrorDialog = false;
        proc.Start();
        proc.WaitForExit();
        success = true;
    }

    catch { }

    Trace.TraceInformation(string.Format("Video has been trimmed") );

    using (Stream file = File.OpenRead(path2))
    {
        Trace.TraceInformation(string.Format("Video moving to CopyStream"));
        copyStream(file, output);
    }
}

Copy Stream: 复制流:

public static void copyStream(Stream input, Stream output)
{
    Trace.TraceInformation(string.Format("Video has been trimmed and now sent to be copied to stream"));
    byte[] buffer = new byte[8 * 1024];
    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, len);
    }
}

Blob mover: Blob移动器:

public static void fileMover (CloudBlob mover, string filePath, Stream input, Stream output)
        {


            string localStoragePath;
            //start string from 7th letter, eg, images
            string link = filePath.Substring(7);


                LocalResource locRes;
                locRes = RoleEnvironment.GetLocalResource("WorkerRoleLocalStorage");

                //To determine the path to the local storage resource's directory

                localStoragePath = string.Format(locRes.RootPath);

            //Moving file to local storage
            try
            {
                mover.DownloadToFile(localStoragePath + link, FileMode.OpenOrCreate);
                Trace.TraceInformation("file mover has been called " + localStoragePath + link);
                //add meta data
            }
            catch(Exception e) { }

            try
            {
                Trimmer(localStoragePath + link, output);
            }
            catch(Exception e) {}
        }

Thank you. 谢谢。

According to your description, I have created my project to test this issue. 根据您的描述,我已经创建了我的项目来测试此问题。 I could make it work as expected both on my side with storage Emulator and deployed to Azure with real storage account. 我可以使它既可以在存储模拟器上按预期工作,又可以通过真实的存储帐户部署到Azure。

在此处输入图片说明

I noticed that you have wrapped your code with try-catch , here are some suggestions for you to locate and solve this issue: 我注意到您已经使用try-catch包装了代码,以下是一些建议,供您查找和解决此问题:

  • You could enable diagnostics for your cloud service project to log your application logs before deploying to Azure. 您可以为云服务项目启用诊断,以便在部署到Azure之前记录应用程序日志。 You could leverage System.Diagnostics.Trace to log the detailed error when you capturing the exception. 捕获异常时,可以利用System.Diagnostics.Trace记录详细的错误。 For more details, you could follow this tutorial . 有关更多详细信息,您可以按照本教程进行操作

  • For Trimmer method, you need to trouble check the path2 file is exist. 对于Trimmer方法,您需要麻烦检查path2文件是否存在。 I have tested that if the file is not generated, then your output blob file would be 0 in size. 我已经测试过,如果未生成该文件,那么您的输出Blob文件的大小将为0。 I modified the Trimmer method, you could refer to it. 我修改了Trimmer方法,您可以参考它。

public static void Trimmer(string localStoragePath, CloudBlockBlob outPutBlob)
{
    string path2 = localStoragePath.Remove(localStoragePath.Length - 4) + "_trimmed.mp4";
    string ffmpeg = "ffmpeg.exe";
    bool success = false;
    string ExeArguments;
    try
    {
        Process proc;
        proc = new Process();
        proc.StartInfo.FileName = ffmpeg;
        ExeArguments = @" -t 10 -i " + localStoragePath + " -map_metadata 0 -acodec copy "
                                        + path2 + " -y";
        Trace.TraceInformation(ExeArguments);
        proc.StartInfo.Arguments = ExeArguments;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.ErrorDialog = false;
        proc.Start();
        proc.WaitForExit();
    }
    catch(Exception ex)
    {
        Trace.TraceError($"Trimmer exception:{ex.Message}\r\nStackTrace:{ex.StackTrace}");
    }

    var fi = new FileInfo(path2);
    if (fi.Exists)
    {
        success = true;
        Trace.TraceInformation(string.Format("Video has been trimmed"));
        using (Stream file = File.OpenRead(path2))
        {
            Trace.TraceInformation(string.Format("Video moving to CopyStream"));
            Trace.TraceInformation(string.Format("Video has been trimmed and now sent to be copied to stream"));
            byte[] buffer = new byte[8 * 1024];
            int len;
            using (Stream outpuStream = outPutBlob.OpenWrite())
            {
                while ((len = file.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outpuStream.Write(buffer, 0, len);
                }
            }    
        }
    }
    else
    {
        Trace.TraceWarning(string.Format("Video has not been trimmed"));
    }
}

Additionally, you could follow this tutorial to view the diagnostics data or you could leverage Microsoft Azure Storage Explorer to check the WADLogsTable of your related storage account to check your application logs. 此外,您可以按照本教程查看诊断数据,也可以利用Microsoft Azure存储资源管理器检查相关存储帐户的WADLogsTable ,以检查应用程序日志。

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

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