Nabawoka
Nabawoka

Reputation: 111

Upload file to AWS S3 using amazonInfo provided by a client

We are integrating with a salesforce client that provided us with an amazonInfo object to use for uploading files to s3 as follows:

"amazonInfo": {
        "UploadId": "a0T1b000000aR41EAF",
        "PolicySigned": "0KtfeVcVYTVWQs3Uj+GjxkB/f8A=",
        "PolicyEncoded": "xyzgICAgImV4cGlyYXRpb24iOiAiMjAyMC0wOC0xNFQxMTo0MjozMS4zODdaIiwKICAgICJjb25kaXWpb25zIjoKICAgIFsKICAgICAgICB7UCJidWNrZXQiOiAiQ2hhcnRzd2FwIiB9LAogICAgICAgIHsgImFjbCI6ICJwcml2YXRlIiB9LAogICAgICAgIHsgImNvbnRlbnQtdHlwZSI6IlNpZ25lZCBBdXRob3JpemF0aW9uIEZvcm0ifSwKICAgICAgICB7ICJ4LWFtei1zZXJ2ZXItc2lkZS1lbmNyeXB0aW9uIjogIkFFUzI1NiJ9LAogICAgICAgIHsgImtleSI6ICJjeC1hMFQxYjAwMDAwMHZSNDFFQUUtMzQ5NjgxLk9DLnRlc3QucGRmIiB9CiAgICBtHs0=",
        "Policy": "{\n    \"expiration\": \"2020-08-14T11:52:31.387Z\",\n    \"conditions\":\n    [\n        { \"bucket\": \"clientBucket\" },\n        { \"acl\": \"private\" },\n        { \"content-type\":\"Signed Form\"},\n        { \"x-amz-server-side-encryption\": \"AES256\"},\n        { \"key\": \"cx-a0T1b000000aR41EAF-349681.OC.test.pdf\" }\n    ]\n}",
        "Key": "AKIAJFOYVDQQZEXAMPLE",
        "FileNameLocal": "349681.OC.test.pdf",
        "FileNameAWS": "cx-a0T1b000000aR41EAF-349681.OC.test.pdf",
        "ErrorMessage": null,
        "EndPoint": "https://s3.amazonaws.com/clientBucket",
        "ContentType": "Signed Form",
        "Acl": "private"
    }

I tried to use this object By browser based upload but I cannot map all fields and it requires a signature that uses a secretKey to be signed with.

I also tried to use the High Level/Low Level Upload but it always requires a secretKey.

Can I use this object to upload the file without a secretKey?

I also wondered What is the use/purpose of UploadId and PolicySigned here?

Upvotes: 0

Views: 313

Answers (1)

Tamer Fouad
Tamer Fouad

Reputation: 21

This info can be used in a Browser-based upload using an HTTP Post with MultipartFormDataContent as followes:

            using (var content = new MultipartFormDataContent(Guid.NewGuid().ToString()))
            {
                content.Add(new StringContent(awsInfo.Acl), "acl");
                content.Add(new StringContent(awsInfo.Key), "AWSAccessKeyId");
                content.Add(new StringContent(awsInfo.ContentType), "content-type");
                content.Add(new StringContent(awsInfo.FileNameAWS), "key");
                content.Add(new StringContent(awsInfo.PolicyEncoded), "policy");
                content.Add(new StringContent(awsInfo.PolicySigned), "signature");
                content.Add(new StringContent("AES256"), "x-amz-server-side-encryption");
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    byte[] fileContents = new byte[fs.Length];
                    content.Add(new StreamContent(new MemoryStream(fileContents)), "file", awsInfo.FileNameLocal);
                }

                return await _client.PostAsync(awsInfo.EndPoint, content);
            }

Note: awsInfo is a deserialized object of the provided "amazonInfo", and _client is a normal System.Net.Http.HttpClient instance.

Upvotes: 1

Related Questions