简体   繁体   English

AWS S3 Javascript SDK-使用浏览器中的密钥和机密从私有存储区下载文件

[英]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.

相关问题 同时获取 - 使用aws sdk从s3存储桶中下载链接和链接到浏览器中查看 - get both - download link and link to view in the browser from s3 bucket using aws sdk 如何在没有 Aws 密钥和秘密的情况下从 S3 公共存储桶读取 zip 文件 - How to read zip file from S3 public bucket without Aws key and secret 来自AWS S3 Bucket的NodeJS下载文件 - NodeJS download file from AWS S3 Bucket 使用浏览器JavaScript SDK检查AWS S3上是否存在文件? - Check if File Exists on AWS S3 Using Browser JavaScript SDK? Javascript从亚马逊s3存储桶下载文件? - Javascript to download a file from amazon s3 bucket? 如何在浏览器中使用 javascript sdk 在 aws s3 存储桶中上传多部分文件 - How to upload multipart files in aws s3 bucket using javascript sdk in browser 使用JavaScript SDK从AWS s3存储桶中获取getObject:“缺少凭证”错误 - getObject from AWS s3 bucket using Javascript SDK: “missing credential” error 您可以在没有 AWS 登录凭证的情况下从 Amazon S3 存储桶下载文件吗 - Can you download a file from Amazon S3 bucket without having AWS login credentials 如何使用 AWS Javascript SDK 从 AWS S3 存储桶中检索对象列表(包括与每个对象关联的元数据)? - How to retrieve list of objects (including the metadata associated with each object) from AWS S3 bucket using AWS Javascript SDK? 如何通过 API 从 S3 存储桶下载私有文件 - How to dowload a private file from S3 bucket through API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM