简体   繁体   中英

AWS SDK C#: null reference exception when trying to upload object to S3 bucket

I have the following code for connecting and uploading to an AWS S3 bucket:

class Program    
{
    public static AmazonS3EncryptionClient encryptionClient;

    public static void Main(string[] args)
    {
        Upload();
    }

    static void Upload()
    {
        var algorithm = new System.Security.Cryptography.ECDsaCng();
        encryptionClient = new AmazonS3EncryptionClient(new EncryptionMaterials(algorithm));

        PutObjectRequest request = new PutObjectRequest();
        request.BucketName = "...";
        request.Key = "...";
        request.InputStream = new MemoryStream(Encoding.Default.GetBytes("..."));
        encryptionClient.PutObject(request);
    }

I am getting a null reference exception at the

encryptionClient.PutObject(request);

line, but I can't find the cause since none of my objects are null.

I am using Visual Studio and the latest versions of the AWSSDK.S3 and AWSSDK.Core packages.

The NPE is related to the usage of the ECDsaCng algorithm. Replacing the line

var algorithm = new System.Security.Cryptography.ECDsaCng();

with

var algorithm = System.Security.Cryptography.RSA.Create();

allows the code to execute as expected. I cannot say why this behavior is present. The stack trace is originating from the EncryptEnvelopeKey in the AWS SDK.

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