[英]AWS S3 Javascript SDK - download file from private bucket with key and secret in browser
I have csv file in S3 private bucket that I have created key to access. 我在创建访问密钥的S3专用存储桶中有csv文件。 I am successfully using key and secret in Python to download the csv file to disk.
我已在Python中成功使用密钥和秘诀将csv文件下载到磁盘。
Now I am trying the AWSJavaScriptSDK to use JavaScript in the browser to retrieve the csv file. 现在,我正在尝试使用AWSJavaScriptSDK在浏览器中使用JavaScript来检索csv文件。
Note this is something I will use only on my computer so not concerned about having key and secret in page. 请注意,这是我将仅在计算机上使用的东西,因此不必担心页面中是否包含密钥和机密。
I have setup CORS properly and am successfully able to get the file name using the script below. 我已经正确设置了CORS,并且能够使用下面的脚本成功获取文件名。 I have also tried a variation of this code that lists all files in bucket which also works.
我还尝试了此代码的变体,该变体列出了存储桶中的所有文件,这些文件也有效。 So I am authenticating and accessing the bucket properly.
因此,我正在正确地验证和访问存储桶。
But I cannot find any non Node.js examples to retrieve the csv file and download it to disk using the AWSJavaScriptSDK for JavaScript in the browser. 但是我找不到任何非Node.js示例来检索csv文件,并使用浏览器中适用于JavaScript的AWSJavaScriptSDK将其下载到磁盘。
<div id="status"></div>
<ul id="objects"></ul>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.6.3.min.js"></script>
<script type="text/javascript">
AWS.config.accessKeyId = 'xxxxxxxxxxxxxxxxx';
AWS.config.secretAccessKey = 'xxxxxxxxxxxxxxxxx';
AWS.config.region = 'us-east-1';
// create the AWS.Request object
var bucket = new AWS.S3();
bucket.getObject({
Bucket: 'xxxxxxxxxxxxxxx',
Key: 'filename.csv'})
.on('success', function(response) {
console.log("Key was", response.request.params.Key );
}).send();
</script>
My ultimate goal is to use the csv file in a D3.js and chart data. 我的最终目标是在D3.js和图表数据中使用csv文件。
How do I get the csv onto disk or even better stream it into the browser? 如何将csv放入磁盘,甚至更好地将其流式传输到浏览器?
Following should print content of object. 以下应打印对象的内容。 Once you have the content, you can either put a tag on screen and dynamically generate click event that downloads the file, or parse the CSV content and use it for D3.
获得内容后,您可以在屏幕上放置标签并动态生成下载文件的点击事件,也可以解析CSV内容并将其用于D3。
bucket.getObject({
"Key": prefix
}, function (error, data) {
if (error) {
return console.log(error);
}
console.log(data.Body.toString());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.