消息:'The bucket policy does not exist',代码:'NoSuchBucketPolicy',

message: 'The bucket policy does not exist', code: 'NoSuchBucketPolicy',

我们正在尝试在 aws 中附加会话策略,但我们收到以下错误 仍然无法弄清楚为什么会出现此错误

我们正在使用 S3 存储桶和安全令牌服务

注意:我们正在获取临时凭据,但策略未附加到角色

Error NoSuchBucketPolicy: The bucket policy does not exist

这是我们的代码示例

var AWS = require('aws-sdk');
const s3 = new AWS.S3();
var sts = new AWS.STS({ apiVersion: '2011-06-15' });

var access_key, secret_access_key, session_token;


const bucketpolicy=
{
 
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1608525393608",
      "Effect": "Allow",
      "Action": "s3:*",
     
      "Resource":"arn:aws:s3:::temp.bucket2/user_id/*"
     
    }
  ]

};
// var myJSON = JSON.stringify(bucketpolicy);
const role = {
  RoleArn: 'arn:aws:iam::xxxxxxxx:role/webClientRole',
  Policy: JSON.stringify(bucketpolicy),
  RoleSessionName: 'my-test-roles',
  DurationSeconds: 3600
};
sts.assumeRole(role, (err, data) => {
  if (err) {

    console.log(err.message);
    return
  }
  console.log(data)
  access_key = data.Credentials.AccessKeyId,
    secret_access_key = data.Credentials.SecretAccessKey,
    session_token = data.Credentials.SessionToken
  console.log(access_key)
  console.log(secret_access_key,)
  console.log(session_token)
  AWS.Credentials({
    region: 'ap-southeast-1',
    accessKeyId: access_key,
    secretAccessKey: secret_access_key,
    sessionToken: session_token
  });
  AWS.config.update({
    region: 'ap-southeast-1',
    accessKeyId: access_key,
    secretAccessKey: secret_access_key,
    sessionToken: session_token
  }
  );
})
 
// call S3 to retrieve policy for selected bucket
s3.getBucketPolicy({Bucket: "bucket_name"}, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data.Policy);
  }
});

您正在尝试检索不存在的存储桶策略。在您应用一个策略之前,存储桶没有策略。您可以使用 putBucketPolicy():

var bucketName = "your_bucket_name";

var bucketPolicy = { 
  "Version": "2012-10-17",
  "Statement": [{
    ...
  }]
};

var params = {
  Bucket: BucketName, 
  Policy: bucketPolicy
};

s3.putBucketPolicy(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else if (data) {
    console.log("Success", data);
  }
});

// Now s3.getBucketPolicy() should return the applied policy