Using AWS Javascript SDK with NativeScript

六眼飞鱼酱① 提交于 2019-12-13 01:29:19

问题


I'm planning to build an app using Nativescript. However, I want to integrate AWS services in the app architecture.

Please, can someone tell me whether I can use AWS JS SDk with nativescript or not. If yes, how?

Thank you.


回答1:


Yes, you can use AWS SDK with NativeScript. I am using it to upload files to S3. You need to install it using

npm i aws-sdk

File upload to AWS S2 example In your component file, import it

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');
    });

P.S. You need to create a Idendity pool in cognito if you don't have one.



来源:https://stackoverflow.com/questions/53695709/using-aws-javascript-sdk-with-nativescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!