Home > Mobile >  Store temp file in .net lambda and then publish to s3Bucket
Store temp file in .net lambda and then publish to s3Bucket

Time:02-28

I am generating a .csv file for further storing in a s3 bucket using .net c# Lambda function.

This is the process i follow:

  1. Generate the .csv and store it in /tmp/ folder of the lambda function execution. In this step im not sure if it is really saving the file in that path.

        //filepath = @"/tmp/test.csv"
        try
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@filepath, true))
            {
    
                file.WriteLine(ID   ","   workItemType   ","   title   ","   assignedTo   ","   state   ","   iterationPath);                    
                Console.WriteLine("Successfully added");
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException(" somethings wrong: ", ex);
        }
    
  2. Upload the file to s3 bucket.

        try
        {
            await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
            {
                BucketName = "mys3bucket",
                Key = "test.csv",
                ContentType = @"/tmp/test.csv"
            });
        await Task.CompletedTask;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception in PutS3Object:"   ex.Message);                ;
        }
    

In this last step i get this error message:

Exception in PutS3Object:The format of value '\tmp\test.csv' is invalid.

What i am doing wrong?

CodePudding user response:

You need to send the data to include in the csv file:

    await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
    {
        BucketName = "mys3bucket",
        Key = "test.csv",
        ContentBody = DATAINSTRINGFORMAT,
        ContentType = @"text/csv"
    });

or as filepath to send:

    await client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
    {
        BucketName = "mys3bucket",
        Key = "test.csv",
        FilePath = FILEPATHONYOURTEMPFOLDER,
        ContentType = @"text/csv"
    });
  • Related