我正在尝试使用Angular JS从Amazon S3 Bucket显示图像。
我的Amazon S3 CORS配置如下: -
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
HTML标记
<img ng-src="{{imgSrc}}">
用于检索图像的Angular JS代码如下: -
$scope.retrieveImage = function()
{
AWS.config.update({
accessKeyId: "MyAccessKey", secretAccessKey: "MySecretKey"
});
var bucket = new AWS.S3({ params: { Bucket: "MyBucket" } });
bucket.getObject({ Key: 'Datetime.png' }, function (err, file) {
$scope.imgSrc= "";
});
}
getObject()方法中的file.Body参数将作为Uint8Array。
请帮助我: -
如果有人能提供工作示例,那将会非常有帮助。
答案 0 :(得分:1)
试试这样:
$scope.retrieveImage = function() {
var mypath = "https://mybucket.s3.amazonaws.com/Datetime.png";
mypath = mypath.substr(1);
AWS.config.update({
accessKeyId: "MyAccessKey",
secretAccessKey: "MySecretKey"
});
var bucketInstance = new AWS.S3();
var params = {
Bucket: "MyBucket",
Key: mypath
}
bucketInstance.getObject(params, function(err, data) {
if (data) {
$scope.imgSrc= "data:image/jpeg;base64," + encode(data.Body);
} else {
console.log('error::', err);
}
});
}