【问题标题】:How can I upload a file from my web UI to directly Amazon S3?如何将文件从我的 Web UI 直接上传到 Amazon S3?
【发布时间】:2019-01-11 04:09:10
【问题描述】:

我有一个 PHP 代码可以将文件从本地系统上传到 Amazon S3。我还有 PHP 网页,可以让用户上传文件并提交。我的要求是我需要将用户上传的文件直接上传到 Amazon S3。有没有办法将文件直接从 UI 上传到 Amazon S3 而无需将其保存在系统中。?如果有办法,我该怎么做。

提前致谢。

【问题讨论】:

    标签: php html amazon-web-services amazon-s3 file-upload


    【解决方案1】:

    是的,您可以在使用 sdk-for-javascript 之间不保留任何系统的情况下做到这一点

    你可以参考 sample web page 来做这件事。

    以下是该页面的代码 sn-p

    function addPhoto(albumName) {
      var files = document.getElementById('photoupload').files;
      if (!files.length) {
        return alert('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 alert('There was an error uploading your photo: ', err.message);
        }
        alert('Successfully uploaded photo.');
        viewAlbum(albumName);
      });
    }
    

    为了从浏览器脚本设置您的 SDK 凭据,以下是按推荐顺序排列的方法:

    1. 使用 Amazon Cognito Identity 对用户进行身份验证并提供凭证
    2. 使用网络联合身份
    3. 脚本中的硬编码

    有关设置凭据的更多详细信息,请参阅 herehere

    【讨论】:

    • 那么授权呢?请注意,您可以使用预签名的网址代表您的帐户上传
    • 非常感谢。我会试试这个,稍后再回来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2010-09-12
    • 2011-09-01
    • 2012-02-02
    相关资源
    最近更新 更多