'AWS is not defined' 在 angular 中使用 aws-sdk-js
'AWS is not defined' when using aws-sdk-js in angular
按照 this 教程,使用 angular 实施 AWS SDK,我从 jshint 获得 AWS is not defined
(使用 grunt 为应用程序提供服务)。
我用 bower install aws-sdk-js --save
安装了 sdk,它正确地出现在我的 index.html 文件中。
这是我的控制器:
angular.module('myApp')
.controller('S3uploadCtrl', function ($scope) {
console.log(AWS);
$scope.creds = {
bucket: 'myBucket',
accessKey: 'accKey',
secretKey: 'secKey'
};
$scope.upload = function() {
// Configure The S3 Object
AWS.config.update({ accessKeyId: $scope.creds.accessKey, secretAccessKey: $scope.creds.secretKey });
AWS.config.region = 'us-west-2';
var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });
if($scope.file) {
var params = { Key: $scope.file.name, ContentType: $scope.file.type, Body: $scope.file, ServerSideEncryption: 'AES256' };
bucket.putObject(params, function(err, data) {
if(err) {
// There Was An Error With Your S3 Config
alert(err.message);
return false;
}
else {
// Success!
alert('Upload Done');
}
})
.on('httpUploadProgress',function(progress) {
// Log Progress Information
console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
});
}
else {
// No File Selected
alert('No File Selected');
}
};
function alert(msg) {
console.alert(msg);
}
});
google 上没有太多关于此的内容。我找到了另一个 SO question,但我试图遵循但无济于事。 (更改了我的 <script>
标签等的顺序)
如果您只是收到 JSHint 错误,可能是因为 AWS 未被识别为变量。在项目的根目录中创建一个 .jshintrc 文件,并将此配置放入其中:
"globals": {
"AWS": false
}
这是一个 JSHint 错误。 JSHint 确保您正在访问定义的变量,并且不知道 AWS 全局变量存在于运行时。所以你需要告诉 JSHint 这个全局变量存在并且你允许你的代码访问这个全局变量(尽管你可能应该将它隐藏在 angular 服务后面,以使你的代码可测试)。
编辑您的 .jshintrc 文件(它可能有另一个名称:检查您的构建配置),并添加(或修改)以下规则:
"globals": { "AWS" : false }
'AWS is not defined' 当你忘记定义 js 时出现这个错误,
"bower install aws-sdk-js" 之后
您需要在
之类的脚本标记中为 index.html 定义 "aws-sdk.min.js" 和 "aws-sdk.js"
<script src="bower_components/aws-sdk/dist/aws-sdk.min.js"></script>
<script src="bower_components/aws-sdk/dist/aws-sdk.js"></script>
按照 this 教程,使用 angular 实施 AWS SDK,我从 jshint 获得 AWS is not defined
(使用 grunt 为应用程序提供服务)。
我用 bower install aws-sdk-js --save
安装了 sdk,它正确地出现在我的 index.html 文件中。
这是我的控制器:
angular.module('myApp')
.controller('S3uploadCtrl', function ($scope) {
console.log(AWS);
$scope.creds = {
bucket: 'myBucket',
accessKey: 'accKey',
secretKey: 'secKey'
};
$scope.upload = function() {
// Configure The S3 Object
AWS.config.update({ accessKeyId: $scope.creds.accessKey, secretAccessKey: $scope.creds.secretKey });
AWS.config.region = 'us-west-2';
var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });
if($scope.file) {
var params = { Key: $scope.file.name, ContentType: $scope.file.type, Body: $scope.file, ServerSideEncryption: 'AES256' };
bucket.putObject(params, function(err, data) {
if(err) {
// There Was An Error With Your S3 Config
alert(err.message);
return false;
}
else {
// Success!
alert('Upload Done');
}
})
.on('httpUploadProgress',function(progress) {
// Log Progress Information
console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
});
}
else {
// No File Selected
alert('No File Selected');
}
};
function alert(msg) {
console.alert(msg);
}
});
google 上没有太多关于此的内容。我找到了另一个 SO question,但我试图遵循但无济于事。 (更改了我的 <script>
标签等的顺序)
如果您只是收到 JSHint 错误,可能是因为 AWS 未被识别为变量。在项目的根目录中创建一个 .jshintrc 文件,并将此配置放入其中:
"globals": {
"AWS": false
}
这是一个 JSHint 错误。 JSHint 确保您正在访问定义的变量,并且不知道 AWS 全局变量存在于运行时。所以你需要告诉 JSHint 这个全局变量存在并且你允许你的代码访问这个全局变量(尽管你可能应该将它隐藏在 angular 服务后面,以使你的代码可测试)。
编辑您的 .jshintrc 文件(它可能有另一个名称:检查您的构建配置),并添加(或修改)以下规则:
"globals": { "AWS" : false }
'AWS is not defined' 当你忘记定义 js 时出现这个错误, "bower install aws-sdk-js" 之后 您需要在
之类的脚本标记中为 index.html 定义 "aws-sdk.min.js" 和 "aws-sdk.js"<script src="bower_components/aws-sdk/dist/aws-sdk.min.js"></script>
<script src="bower_components/aws-sdk/dist/aws-sdk.js"></script>