我目前正在使用javascript SDK访问我的s3存储桶,它可以正常工作,但是我直接在javascript中硬编码了所有凭据,但在SDK中他们说你可以将这些凭据存储在AWS.config对象中,而我不知道如何做到这一点。此外,在线资源也没有提供足够的信息,因此请问有人能告诉我如何做到这一点或者是否有其他更好的方法来代替硬编码凭据?
<script type="text/javascript">
AWS.config.accessKeyId = 'dddddddddd';
AWS.config.secretAccessKey = 'rrrrrrrrrrrrrrrreeeeeeeeeeeeeeeeeeeee';
AWS.config.region = 'us-east-1';
// create the AWS.Request object
var bucket = new AWS.S3({ params: { Bucket: 'some.bucket' } });
bucket.listObjects(function (err, data) {
if (err) {
document.getElementById('status').innerHTML =
'Could not load objects from S3';
} else {
document.getElementById('status').innerHTML =
'Loaded ' + data.Contents.length + ' items from S3';
for (var i = 0; i < data.Contents.length; i++) {
document.getElementById('objects').innerHTML +=
'<li>' + data.Contents[i].Key + '</li>';
}
}
});
</script>
accessKeyId
和secretAccessKey
已经被弃用。我选择使用这种方法:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-json-file.html - Kevin Danikowski