使用react-native-aws-signature将图像上传到AWS

r6hnlfcb  于 2023-05-18  发布在  React
关注(0)|答案(2)|浏览(99)

我试图上传一个图像到亚马逊s3使用react-native-aws-signature,这里是我的样本代码,我附上

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 1在响应中。uri是在身体是来自图像拾取器传递,可以任何一个给我的代码中有任何错误的建议,如果有请告诉我,如何解决它,任何帮助非常感谢

nzkunb0c

nzkunb0c1#

您还可以在官方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

g6ll5ycj

g6ll5ycj2#

awsSignature.getAuthorizationHeader();将在给定正确的参数时返回authorization header,这就是它的全部功能。这只是对AWS API进行签名调用的整个过程中的一个步骤。
当向S3发送POST请求时,这里有一个指向您应该阅读的官方文档的链接。S3 Documentation看起来你需要把图像作为表单参数发送进来。

相关问题