使用JavaScript将使用FileSystem API创建的文件上载到S3存储桶

时间:2018-01-03 10:00:44

标签: javascript amazon-web-services file-upload amazon-s3 html5-filesystem

当我尝试将文件(FILE_PATH)上传到S3存储桶时,它正在上传,但我将文件路径(FILE_PATH)作为文件中的内容。请问你能说出我做错了吗?

我按照此链接https://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file创建了一个文件并写入其中。

除了我将文件路径(FILE_PATH)作为上传文件中的内容

之外,一切似乎都有效

这是我的代码。

var FILE_PATH = 'filesystem:http://localhost:5000/temporary'

  function onInitFs(fs) {
    fs.root.getFile('log.json', {create: true}, function(fileEntry) {

      fileEntry.createWriter(function(fileWriter) {

        fileWriter.onwriteend = function(e) {
          console.log('Write completed.');
        };

        fileWriter.onerror = function(e) {
          console.log('Write failed: ' + e.toString());
        };

        var sukshi_dict = {
          "sfds": "sdfse"
        }

        var sukshi_dict = JSON.stringify(sukshi_dict);
        var blob = new Blob([sukshi_dict], {type: "application/json"});
        fileWriter.write(blob);
        addPhoto('hero', FILE_PATH)
      }, errorHandler);
      FILE_PATH = FILE_PATH + fileEntry.fullPath
      console.log(FILE_PATH)
    }, errorHandler);

  }

  window.requestFileSystem(window.TEMPORARY, 5*1024*1024 /*5MB*/, onInitFs, errorHandler);

function addPhoto(albumName, file_path) {
    debugger
    var file = file_path;
    if (!file.length) {
      return alert('Please choose a file to upload first.');
    }
    var fileName = file.substring(file.lastIndexOf('/')+1);
    console.log(fileName)
    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.');
    });
  }

0 个答案:

没有答案