简体   繁体   English

使用客户端提供的 amazonInfo 将文件上传到 AWS S3

[英]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:我们正在与 salesforce 客户端集成,该客户端为我们提供了 amazonInfo object 用于将文件上传到 s3,如下所示:

"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.我尝试通过基于浏览器的上传来使用这个 object,但我不能 map 的所有字段,它需要一个使用密钥进行签名的签名。

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?我可以使用这个 object 来上传没有密钥的文件吗?

I also wondered What is the use/purpose of UploadId and PolicySigned here?我还想知道 UploadId 和 PolicySigned 的用途/目的是什么?

This info can be used in a Browser-based upload using an HTTP Post with MultipartFormDataContent as followes:此信息可用于使用 HTTP Post 和 MultipartFormDataContent 的基于浏览器的上传,如下所示:

            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.注意: awsInfo 是提供的“amazonInfo”的反序列化 object,_client 是普通的 System.Net.Http.HttpClient 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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