I want to add string value from the database to the string with @ as seen at the code below. However, I keep getting an error such as ; expected and I try to find the answer through other post but it does not seem like there is not. Is there any way I can input the value inside the string with @?
Here is the code :
string name = bucketName; //the value that I want to put inside the newPolicy at resource part
// Put sample bucket policy (overwrite an existing policy)
string newPolicy = @"{
""Statement"":[{
""Sid"":""PublicReadGetObject"",
""Effect"":""Allow"",
""Principal"": ""*"",
""Action"":[""s3:GetObject""],
""Resource"":[""arn:aws:s3:::"//the string name value insert here"/*""]}]}";
Any help will be appreciated
Thank You
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you use that string as an input to string.Format() then you will need to escape the “{” and “}” by writing them as “{{” and “}}” respectively, like this:
string name = "XYZ"; //the value that I want to put inside the newPolicy at resource part
// Put sample bucket policy (overwrite an existing policy)
string newPolicy = @"{{
""Statement"":[{{
""Sid"":""PublicReadGetObject"",
""Effect"":""Allow"",
""Principal"": ""*"",
""Action"":[""s3:GetObject""],
""Resource"":[""arn:aws:s3:::{0}""]}}]}}";
string s = string.Format(newPolicy, name);
Whereupon the value of s is:
{
"Statement":[{
"Sid":"PublicReadGetObject",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::XYZ"]}]}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0