问题
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