繁体   English   中英

无法在容器启动时更改某些 js/html 文件内容。 获取访问被拒绝异常

[英]Unable to change some js/html file content on container start. getting access denied exception

我有一个案例,我需要在编译的 vuejs js/html 文件中注入一些环境变量值,该文件驻留在我的 .net core 3.1 应用程序中。 我已经将内容更改逻辑放在了启动.cs 的配置方法中。 我收到拒绝访问异常我的代码是

private void ReplaceEnvironmentVariableWithVal()
        {
            try
            {
                string apiurlToken = "VUE_APP_BEAPI_URL";
                string relevantPath = "VUE_APP_RELEVANTPATH";
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "clientapp", "dist", "js");
                Console.WriteLine($"{apiurlToken} env value is {GetEnvVariable(apiurlToken)}");
                Console.WriteLine($"{relevantPath} env value is {GetEnvVariable(relevantPath)}");
                Console.WriteLine("Application Root Path of js files " + path);
                DirectoryInfo dir = new DirectoryInfo(path);
                Dictionary<string, string> dict = new Dictionary<string, string>();
                if (dir != null)
                {
                    foreach (var file in dir.GetFiles())
                    {
                        if (file.Extension == "js" || file.Extension == ".js")
                        {
                            var fileContent = File.ReadAllText(file.FullName);
                            if (!String.IsNullOrWhiteSpace(fileContent))
                            {

                                if (fileContent.Contains(apiurlToken))
                                {
                                    string variableValue = GetEnvVariable(apiurlToken);
                                    if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(apiurlToken, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }

                                }

                                if (fileContent.Contains(relevantPath))
                                {
                                    string variableValue = GetEnvVariable(relevantPath);
                                    //if (!String.IsNullOrWhiteSpace(variableValue))
                                    {
                                        fileContent = fileContent.Replace(relevantPath, variableValue);
                                        dict[file.FullName] = fileContent;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Application Root Path of js files not exists");
                }
              
                foreach (KeyValuePair<string, string> entry in dict)
                {
                    Console.WriteLine("Updating " + entry.Key);
                    using (var fs = new FileStream(entry.Key, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                    {
                        using var sr = new StreamWriter(fs);
                        sr.Write(entry.Value);
                    }
                    Console.WriteLine("Update Completed " + entry.Key);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in ReplaceEnvironmentVariableWithVal:" + ex.Message);
            }
        }

我已经实现了我赋予用户权限的文件的写入。 我已经记录了运行应用程序的用户,它是默认的。 然后在我的 docker 文件 COPY --chown=default:default 中添加了以下行。 .

基本上赋予用户权利

暂无
暂无

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

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