【问题标题】:How to get the local file when uploading it to amazon aws s3?上传到amazon aws s3时如何获取本地文件?
【发布时间】:2019-03-07 17:07:12
【问题描述】:

这是我在网上找到的解决方案:[https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html]

function addPhoto(albumName) {
  var files = document.getElementById('photoupload').files;
  if (!files.length) {
    return console.log('Please choose a file to upload first.');
  }
  var file = files[0];
  var fileName = file.name;
  var albumPhotosKey = encodeURIComponent(albumName) + '//';

  var photoKey = albumPhotosKey + fileName;
  s3.upload({
    Key: photoKey,
    Body: file,
    ACL: 'public-read'
  }, function(err, data) {
    if (err) {
      return console.log('There was an error uploading your photo: ', err.message);
    }
    console.log('Successfully uploaded photo.');
    viewAlbum(albumName);
  });
}

但是,在我当前的环境中,没有所谓的“文档”这个概念。我真的不知道“文档”是如何工作的。我可以在当前环境中包含“文档”吗?或者我可以使用其他东西来获取本地文件[图像]?非常感谢!

s3.上传:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    您应该指定您的环境是什么。 document 对象仅在 HTML 中有意义,HTML 是在浏览器中运行的网页。如果你不是在浏览器中运行,而是独立运行,你可能会使用 Node.js。

    正如documentation 所说,Body 参数可以是 Buffer、Typed Array、Blob、String 或 ReadableStream。

    因此,在 Node.js 中简单上传本地文件可能如下所示:

    var fs = require('fs');
    var stream = fs.createReadStream('/my/file');
    
    s3.upload({
       Bucket: 'mybucket',
       Key: 'myfile'
       Body: stream
    }, function(err, data) {
       if (err) return console.log('Error by uploading.', err.message);
       console.log('Successfully uploaded.');
    });
    

    【讨论】:

    • 非常感谢!这就是我要找的。​​span>
    【解决方案2】:

    文档对象模型 (DOM) 是一种跨平台且独立于语言的应用程序编程接口,它将 HTML、XHTML 或 XML 文档视为树结构,其中每个节点都是代表文档一部分的对象。

    DOM 表示具有逻辑树的文档。树的每个分支都以一个节点结束,每个节点都包含对象。 DOM 方法允许以编程方式访问树;使用它们可以改变文档的结构、样式或内容。节点可以附加事件处理程序。一旦事件被触发,事件处理程序就会被执行

    来源: https://en.wikipedia.org/wiki/Document_Object_Model

    这只是页面加载到浏览器后用于访问 DOM 的根上下文。

    查看文档模型加载事件的示例:

    document.addEventListener("DOMContentLoaded", function(event) {
        // - Code to execute when all DOM content is loaded. 
        alert("LOADED!");
    });

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2012-06-14
      • 2014-02-03
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多