首页 > 解决方案 > 如何将文件从我的 Web UI 直接上传到 Amazon S3?

问题描述

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

提前致谢。

标签: phphtmlamazon-web-servicesamazon-s3file-upload

解决方案


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

您可以参考执行此操作的示例网页

以下是该页面的代码片段

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. 使用 Web 联合身份
  3. 在脚本中硬编码

可以在此处此处找到有关设置凭据的更多详细信息


推荐阅读