简体   繁体   中英

AWS C# cant figure out how to upload zip stream to S3 bucket

I am currently in the process of trying to put 3rd party API data into a S3 bucket via a C# AWS Lambda function. The data from the 3rd party comes in a zip file that I have been able to save locally fine so I am assuming that is working as I need it to. The next step is to upload this data to a S3 bucket but via a stream rather than saving the file locally and uploading etc.

public static async void PutObjectAsync()
    {
        try
        {
            using (var client = new AmazonS3Client(Amazon.RegionEndpoint.EUWest2))
            {

                var ms = new MemoryStream();
                GetStream().CopyTo(ms);    //GetStream() returns the api data stream (Stream object).

                var request = new PutObjectRequest()
                {
                    BucketName = "*BUCKETNAME*",
                    Key = "data.zip",
                    InputStream = ms

                };

                var response = await client.PutObjectAsync(request);                  

            }
        }
        catch (AmazonS3Exception e)
        {
            Console.WriteLine(
                    "Error encountered ***. Message:'{0}' when writing an object"
                    , e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine(
                "Unknown encountered on server. Message:'{0}' when writing an object"
                , e.Message);
        }

        Console.ReadKey();
    }
 }

I get a object reference not set to reference of an object error on the PutObjectAsync function call. I have inspected the request and the memory stream appears to be loaded as expected from the CopyTo method. I am aware that buckets have issues with files over 5MB but the zip file is a lot smaller than 5MB.

This is my first lambda function so any advice would be welcomed. Thanks in advance.

As diagnosed in the comments, AWS requires credentials to allow access to the S3 Bucket. Unfortunately it doesnt mention in the documentation that these are required, and are by default looked up from disk as described at https://stackoverflow.com/a/43243290/784908 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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