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

时间:2015-12-07 06:38:27

标签: javascript angularjs ng-file-upload

我目前能够在s3的根存储桶下上传文件,但是我想指定它应该保存的文件夹。将文件夹名称附加到网址会产生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 config

<?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/*"
  }]
}

1 个答案:

答案 0 :(得分:3)

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

  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文件夹更像是概念,而不是实际的资源类型。