javascript - 如何将图像保存在特定文件夹 s3 下?

标签 javascript angularjs ng-file-upload

我目前可以在 s3 的根存储桶下上传文件,但是我想指定它应该保存到的文件夹。将文件夹名称附加到 url 会产生 405。

上传

$scope.s3upload = function(file) {
  Upload.upload({
    url: 'https://s3.amazonaws.com/XXX/',
    method: 'POST',
    data: {
      key: file.name,
      AWSAccessKeyId: "XXX",
      acl: 'public-read',
      policy: "XXX",
      signature: "XXX",
      "Content-Type": file.type != '' ? file.type : 'application/octet-stream',
      filename: file.name,
      polyfill IE8 - 9
      file: file
    }
  });
}

S3 CORS 配置

<?xml version="1.0" encoding="UTF-8"?>
    <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

S3 存储桶策略

{
  "Version": "2008-10-17",
  "Id": "Policy1397632521960",
  "Statement": [{
    "Sid": "Stmt1397633323327",
    "Effect": "Allow",
    "Principal": {
      "AWS": "*"
    },
    "Action": [
      "s3:GetObject",
      "s3:PutObject"
    ],
    "Resource": "arn:aws:s3:::XXX/*"
  }]
}

最佳答案

您应该将文件夹名称指定为中的前缀,即

  key: "folder/" + file.name,
  AWSAccessKeyId: "XXX",
  acl: 'public-read',
  policy: "XXX",
  signature: "XXX",
  "Content-Type": file.type != '' ? file.type : 'application/octet-stream',
  filename: file.name,
  polyfill IE8 - 9
  file: file

与大多数对象存储实现一样,Amazon S3 文件夹更像是一个概念,而不是实际的资源类型。

关于javascript - 如何将图像保存在特定文件夹 s3 下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34127801/

相关文章:

javascript - Highcharts 错误 #13 和 AngularJs

javascript - 合并愚蠢的 jquery 代码

rest - 为什么angular app调用rest服务时端口号被剪掉了

angularjs - Angular UI 路由器 |多个 View : How to use the resolve object in multiple views?

javascript - 如何将 jQuery fadeIn 滚动添加到 Angular 应用程序

angularjs - 跨域图片上传 Angular+laravel

javascript - 使用工厂作为 Angular 数据表中的源数据不起作用

javascript - 简单的 Javascript HTML 替换

AngularJs 图片上传到 S3

javascript - 打开用户选择的文件