首页 > 解决方案 > 将 AWS Javascript SDK 与 NativeScript 结合使用

问题描述

我打算使用 Nativescript 构建一个应用程序。但是,我想将 AWS 服务集成到应用程序架构中。

请有人告诉我是否可以将 AWS JS SDK 与 nativescript 一起使用。如果是,如何?

谢谢你。

标签: aws-sdknativescript

解决方案


是的,您可以将 AWS 开发工具包与 NativeScript 结合使用。我正在使用它将文件上传到 S3。您需要使用安装它

npm i aws-sdk

文件上传到 AWS S2 示例 在您的组件文件中,将其导入

import * as AWS from 'aws-sdk';

const AWSService = AWS;
    const region = 'Your_Region_name';
    this.bucketName = 'bucketName ';
    const IdentityPoolId = 'IdentityPoolId';

    // Configures the AWS service and initial authorization
    AWSService.config.update({
      region: region,
      credentials: new AWSService.CognitoIdentityCredentials({
        IdentityPoolId: IdentityPoolId
      })
    });

    // adds the S3 service, make sure the api version and bucket are correct
    this.s3 = new AWSService.S3({
      apiVersion: '2006-03-01',
      params: { Bucket: this.bucketName }
    });
this.s3.upload({ Key: 'test/' + file.name, Bucket: this.bucketName, Body: file, ACL: 'public-read' }, function (err, data) {
      if (err) {
        console.log(err, 'there was an error uploading your file');
      }
      console.log('upload done');
    });

PS 如果您没有身份池,则需要在 cognito 中创建一个身份池。


推荐阅读