Christopher Martinez
Christopher Martinez

Reputation: 827

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

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?

Upvotes: 1

Views: 1128

Answers (1)

J.Salas
J.Salas

Reputation: 1363

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"
    });

Upvotes: 2

Related Questions