使用x-amz-tagging请求标头

时间:2017-03-23 06:11:40

标签: javascript node.js amazon-web-services amazon-s3 http-post

我有一个用于文件上传到aws s3的Node程序,我需要用请求头指定x-amz-tagging。我尝试了一些东西,但它没有用!



function buildRequestHeader() {
  return {
    'Content-Length': fileBuffer.size,
    'Content-Type': mimeType,
    'x-amz-acl': 'public-read',
    'x-amz-tagging' :{"tag1":'abcd',"tag2":'efgh'}
  }
}




我从aws docs中看到了一些东西,



PUT object-key?tagging HTTP/1.1
Host: examplebucket.s3.amazonaws.com
Content-Length: length
Content-MD5: pUNXr/BjKK5G2UKExample==
x-amz-date: 20160923T001956Z
Authorization: authorization string
<Tagging>
   <TagSet>
      <Tag>
         <Key>tag1</Key>
         <Value>val1</Value>
      </Tag>
      <Tag>
         <Key>tag2</Key>
         <Value>val2</Value>
      </Tag>
   </TagSet>
</Tagging>
&#13;
&#13;
&#13;

你能解释它是如何运作的吗?

2 个答案:

答案 0 :(得分:3)

您展示的代码段来自两种不同的标记方法,第一种是object creation,第二种是adding tags to an existing object

x-amz-tagging标题是您在对象创建过程中使用的标题,但它使用different syntax标题而不是示例。试试这个:

function buildRequestHeader() {
  return {
    'Content-Length': fileBuffer.size,
    'Content-Type': mimeType,
    'x-amz-acl': 'public-read',
    'x-amz-tagging': 'tag1=abcd&tag2=efgh'
  }
}

答案 1 :(得分:1)

&#13;
&#13;
const fs= require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3({'accesskey':'accesskeyvalue','region':'your s3 region'})

s3.upload(
  {
    Bucket: bucket_name,
    Key: key,
    Tagging: 'tag1=abcd&tag2=efgh',
    Body: fs.createReadStream('./filename.filetype'),
  }
&#13;
&#13;
&#13;

这种方法也有效!!