使用react-native-aws-signature

时间:2016-03-17 13:36:29

标签: amazon-web-services amazon-s3 react-native

hai我尝试使用react-native-aws-signature将图像上传到Amazon-s3,这是我附加的示例代码

var AWSSignature = require('react-native-aws-signature');
var awsSignature = new AWSSignature();
var source1 = {uri: response.uri, isStatic: true}; // this is uris which got from image picker
        console.log("source:"+JSON.stringify(source1));
        var credentials = {
          SecretKey: ‘security-key’,
          AccessKeyId: ‘AccesskeyId’,
          Bucket:’Bucket_name’
        };
        var options = {
          path: '/?Param2=value2&Param1=value1',
            method: 'POST',
            service: 'service',
            headers: {
                'X-Amz-Date': '20150209T123600Z',
                'host': 'xxxxx.aws.amazon.com'
            },
          region: ‘us-east-1,
          body: response.uri,
          credentials
        };
        awsSignature.setParams(options);
        var signature = awsSignature.getSignature();
        var authorization = awsSignature.getAuthorizationHeader();

这里我声明source.in中的source1传递来自图像选择器的正文,任何人都可以提出我的代码中有任何错误的建议,如果有请告诉我如何解决它,任何帮助非常感谢

2 个答案:

答案 0 :(得分:0)

在给定正确的参数时,

awsSignature.getAuthorizationHeader();将返回授权标头,并且完全是这样。只需在对AWS API进行签名调用的整个过程中迈出一步。

向S3发送POST请求时,这里是您应该阅读的官方文档的链接。 S3 Documentation 您似乎需要将图像作为表单参数发送。

答案 1 :(得分:0)

您还可以在此处的官方AWS仓库中使用新的AWS Amplify库:https://github.com/aws/aws-amplify

这有一个存储模块,用于对S3的请求进行签名:https://github.com/aws/aws-amplify/blob/master/media/storage_guide.md

对于React Native,您需要安装:

npm install aws-amplify-react-native

如果您使用的是Cognito用户池凭据,则需要按照此处所述链接原生网桥:https://github.com/aws/aws-amplify/blob/master/media/quick_start.md#react-native-development