问题
I am trying to retrieve the URL of a picture from Amazon s3.
When I run the script below, I get an error:
Missing required Key in params
This is what I have so far:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.16.min.js"></script>
<script type="text/javascript">
function test1(){
AWS.config.update({
accessKeyId: 'accesskey',
secretAccessKey: 'secretKey'
});
AWS.config.region = 'us-west-2';
var myAWS = new AWS.S3();
myAWS.getObject(
{ Bucket: 'productissues', key: 'carlos.jpg' },
function (error, data) {
if (error != null) {
alert("Failed to retrieve an object: " + error);
} else {
alert("Loaded " + data.ContentLength + " bytes");
// do something with data.body
}
});
}
</script>
</head>
<body>
<button type="button" onclick="test1();" >Click me!</button>
</body>
</html>
回答1:
Please go through this url for configuring your sdk first with proper parameters.
http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html
This part of your code is causing the error
AWS.config.update({
accessKeyId: 'accesskey',
secretAccessKey: 'secretKey'
});
You need to give your accessKeyId and secretAccessKey instead of defaulting them to accesskey and secretkey. They should be replaced with required values following the above url.
Also add 'Key' instead of 'key' in getObject params.
来源:https://stackoverflow.com/questions/25896259/retrieve-data-from-amazon-s3