将 AWS Javascript SDK 与 NativeScript 结合使用
Using AWS Javascript SDK with NativeScript
我打算使用 Nativescript 构建一个应用程序。但是,我想将 AWS 服务集成到应用程序架构中。
拜托,有人可以告诉我是否可以将 AWS JS SDK 与 nativescript 一起使用。
如果是,如何?
谢谢。
是的,您可以将 AWS SDK 与 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');
});
P.S。如果您没有,则需要在 cognito 中创建一个身份池。
我打算使用 Nativescript 构建一个应用程序。但是,我想将 AWS 服务集成到应用程序架构中。
拜托,有人可以告诉我是否可以将 AWS JS SDK 与 nativescript 一起使用。 如果是,如何?
谢谢。
是的,您可以将 AWS SDK 与 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');
});
P.S。如果您没有,则需要在 cognito 中创建一个身份池。