2013-10-12 9 views
5

次のコードでは、AWSアカウント番号を使用してアクセス許可を追加できますが、キューはSNSからのメッセージを受信しません。AWS SQSキューに権限を追加するにはどうすればよいですか?

AddPermissionRequest addPermissionRequest = new AddPermissionRequest(); 
addPermissionRequest.ActionName.Add("SendMessage"); 
addPermissionRequest.ActionName.Add("ReceiveMessage"); 
addPermissionRequest.QueueUrl = queueUrl; 
addPermissionRequest.Label = General.IpAddressAWSFriendly; 
addPermissionRequest.AWSAccountId.Add(AWS_ACCOUNT_ID); 
sqs.AddPermission(addPermissionRequest); 

しかし、私は皆のためのワイルドカード(*)を経由してアクセス権を設定しよう:

addPermissionRequest.AWSAccountId.Add("*"); 

それは私にエラーを与えます。私は手動でAWS SQSコンソールにアクセス許可を追加し、許可されたアクションのためにとの原則のため

SendMessage 
ReceiveMessage 

指定した場合、私は「誰もが」、キューが私のSNSのトピックからメッセージを受信しないに設定します。だから、明らかに、私は間違ったことをやっているが、私はもうそれを見ない。

助けがあれば助かります。 Amazonには例があるといいが、SDKに付属のサンプルには、ポリシーやアクセス許可の設定に関する情報は表示されません。オンラインマニュアルには何も表示されていません。挫折。

答えて

10

幸いにも、私はここで応答を受けていないので、私はそれを自分で考え出しました。私は本当にAmazonがC#開発者向けに.Netフレームワーク上でより良いドキュメンテーションを提供してくれることを願っています。

とにかく、私は完全に吹き飛ばされたPolicyオブジェクトを作成し、それをSQSに渡しました。私はAWS SDK v1.5バージョンを使用していましたが、それは今のところうまくいき、新しい2.0バージョンに変更するのが面倒だったからです。ここで

は、あなたがプログラムでのみSNSトピックにそのキューを使用することが条件とSQSキューのためのC#でポリシーを作成する必要がありますコードのビットは次のとおりです。

 // 4. Set the queue policy to allow SNS to publish messages 
     ActionIdentifier[] actions = new ActionIdentifier[2]; 
     actions[0] = SQSActionIdentifiers.SendMessage; 
     actions[1] = SQSActionIdentifiers.ReceiveMessage; 
     Policy sqsPolicy = new Policy() 
      .WithStatements(new Statement(Statement.StatementEffect.Allow) 
           .WithPrincipals(Principal.AllUsers) 
           .WithResources(new Resource(queueArn)) 
           .WithConditions(ConditionFactory.NewSourceArnCondition(_AWSSNSArn)) 
           .WithActionIdentifiers(actions)); 
     SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest(); 
     List<Amazon.SQS.Model.Attribute> attributes = new List<Amazon.SQS.Model.Attribute>(); 
     Amazon.SQS.Model.Attribute attribute = new Amazon.SQS.Model.Attribute(); 
     attribute.Name = "Policy"; 
     attribute.Value = sqsPolicy.ToJson(); 
     attributes.Add(attribute); 
     setQueueAttributesRequest.QueueUrl = queueUrl; 
     setQueueAttributesRequest.Attribute = attributes; 
     sqs.SetQueueAttributes(setQueueAttributesRequest); 

私は、これは誰かに役立ちます願っています。ここで

+0

ただ、それは一例に来るとき、私はあなたではない膨大な量に同意すると言って - ReceiveMessageWaitTimeSecondsをし、私はちょうどレンガの壁に当たって保つ - 私は、設定方法に探していまし - 私はコンソールでそれを行うことができますが、それを設定する方法のコード例を見つけることはできません - 私はそれがまっすぐになるだろうと思った! – anna

+0

@anna SQSキューで長いポーリングをしようとしているようです。あなたはどんな特定の問題にぶつかりますか?あなたはSOの質問を投稿しましたか?多分私は助けることができます。 –

+0

私は長いポーリングをしようとしている私は手動でそれを設定する方法を知っているが、私は私がキュー内のキューを作成するときに設定する必要があります私はキューを作成するこれを持っているvar sqsRequest = new CreateQueueRequest(); sqsRequest.QueueName = "Bin2AutomationQ"; var createQueueResponse = sqs.CreateQueue(sqsRequest); myQueueUrl = createQueueResponse.CreateQueueResult。QueueUrl; ReceiveMessageWaitTimeSecondsを20に設定する方法がわからない@ – anna

1

amazonicaとClojureの中でそれを行う方法は次のとおりです。ここで

(require '[amazonica.aws.sqs :as sqs] 
     '[amazonica.core :as aws) 
(import '(com.amazonaws.auth.policy Statement Statement$Effect 
            Principal 
            Policy 
            Resource 
            Condition 
            Action) 
     '(com.amazonaws.auth.policy.actions SQSActions) 
     '(com.amazonaws.auth.policy.conditions ConditionFactory)) 

(aws/defcredential "access-key" "secret-key" "us-east-1") 

(def topic-arn "arn:aws:sns:us-east-1:123:foo") 
(def queue-url "https://sqs.us-east-1.amazonaws.com/123/bar") 
(def queue-arn (-> queue-url sqs/get-queue-attributes :QueueArn)) 

(def policy (Policy. 
        (str queue-arn "/SQSDefaultPolicy") 
        [(doto (Statement. Statement$Effect/Allow) 
         (.setPrincipals [Principal/AllUsers]) 
         (.setResources [(Resource. queue-arn)]) 
         (.setConditions [(ConditionFactory/newSourceArnCondition topic-arn)]) 
         (.setActions [SQSActions/SendMessage]))])) 

(sqs/set-queue-attributes queue-url {"Policy" (.toJson policy)}) 
0

は私が匿名でその場でキューを作成する方法であるアクセスをお読みください。必要に応じてActionIdentifierを追加してください:

public static String TestQueueCreate(String name) { 

     AmazonSQSClient sqs = new AmazonSQSClient(region: Amazon.RegionEndpoint.USEast1); 
     CreateQueueResponse create = sqs.CreateQueue(name); 

     Policy policy = new Policy() { 
      Statements = new List<Statement>() { 
       new Statement(StatementEffect.Allow) { 
        Principals = new List<Principal>() { Principal.AllUsers }, 
        Actions = new List<ActionIdentifier>() { SQSActionIdentifiers.ReceiveMessage } 
       } 
      } 
     }; 

     Dictionary<String,String> queueAttributes = new Dictionary<String, String>(); 
     queueAttributes.Add(QueueAttributeName.Policy.ToString(), policy.ToJson()); 
     sqs.SetQueueAttributes(new SetQueueAttributesRequest(create.QueueUrl, queueAttributes)); 

     return create.QueueUrl; 
    } 
1

以前は、期待したとおりに動作しなかったリソースなしのアクセス許可を作成しました。ここでARNを取得するための補正である:

public static String TestQueueCreate(String name) { 

     AmazonSQSClient sqs = new AmazonSQSClient(region: Amazon.RegionEndpoint.USEast1); 
     CreateQueueResponse create = sqs.CreateQueue(name); 

     String arn = sqs.GetQueueAttributes(create.QueueUrl, new List<String>() { "QueueArn" }).Attributes["QueueArn"]; 

     Policy policy = new Policy() { 
      Statements = new List<Statement>() { 
       new Statement(StatementEffect.Allow) { 
        Principals = new List<Principal>() { new Principal("*") }, 
        Actions = new List<ActionIdentifier>() { 
         new ActionIdentifier("SQS:ReceiveMessage"), 
         new ActionIdentifier("SQS:SendMessage") 
        }, 
        Resources = new List<Resource>() { new Resource(arn) } 
       } 
      }, 

     }; 

     Dictionary<String,String> queueAttributes = new Dictionary<String, String>(); 
     queueAttributes.Add(QueueAttributeName.Policy.ToString(), policy.ToJson()); 
     sqs.SetQueueAttributes(new SetQueueAttributesRequest(create.QueueUrl, queueAttributes)); 

     return create.QueueUrl; 
    } 
関連する問題