I am facing a problem when creating a .csv file on the AWS S3 bucket. I have tried the PutObjectRequest and the TransferUtility libraries.
I think it is how I'm writing to the csvText.csv file. The data is written in memory so I can't store the data physically in a file on the server.
Issue: The file (userData.csv) is created but doesn't contain any data.
When I run this application locally on my pc it will create and populate the file in S3. But not the application is called through the AWS URL, the file is created but no data is populated in the csv.
Environment and workflow: My .NET web application (GatherUserData) sits on a EC2 instance which is called by a CreateUser() method from a thirdparty application(CS).
My application then calls (CS) with a GET request. GETS user data and then Creates a CSV file and places it in a S3 bucket.
My code:
public string ExportUserDataCSVFile(GetUserData userData)
{
csvText = new StringBuilder();
try
{
Data1.Customfield[] customeFields = userData.data.customFields;
if (userData != null)
{
//header
csvText.AppendLine(GetSingleUserCSVHeader());
//UserID
csvText.Append(userData.data.userId);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return csvText.ToString();
}
CreateCsvInAws class:
public static void CreateCsvInAws(string csvText)
{
try
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(csvText.ToCharArray());
bw.Flush();
bw.Close();
s3Client = new AWSClient();
PutObjectRequest pr = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
ContentBody = csvText, {I can see the user data here, but it doesn't get written to the file}
ContentType = "application/octet-stream",
CannedACL = "bucket-owner-full-control"
};
PutObjectResponse response = s3Client.PutObject(pr);
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered '{0}' when writing");
}
catch (Exception e)
{
Console.WriteLine("unknown error '{0}' again");
}
}