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

How to save images under a specific folder s3?

我目前可以在 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 文件夹更像是一个概念,而不是实际的资源类型。