简体   繁体   English

为什么 snsClient.unsubscribe(request) 在 snsClient.unsubscribe(request) 上给我一个错误?

[英]Why snsClient.unsubscribe(request) did give me a error on snsClient.unsubscribe(request)?

I'm trying to use:我正在尝试使用:

  UnsubscribeRequest request = UnsubscribeRequest.builder()
                .subscriptionArn("arn:aws:sns:us-east-1:9999999999:myTopic")
                .build();

  UnsubscribeResponse result = snsClient.unsubscribe(request);

But I received an error:但是我收到一个错误:

software.amazon.awssdk.services.sns.model.InvalidParameterException: Invalid parameter: SubscriptionId (Service: Sns, Status Code: 400, Request ID: 08d4fa07-be89-5b3d-bd81-a07bcf09de7a, Extended Request ID: null) at software.amazon.awssdk.core.internal.http.CombinedResponseHandler.handleErrorResponse(CombinedResponseHandler.java:123) ~[sdk-core-2.17.46.jar:na] software.amazon.awssdk.services.sns.model.InvalidParameterException:无效参数:SubscriptionId(服务:Sns,状态代码:400,请求 ID:08d4fa07-be89-5b3d-bd81-a07bcf09de7a,扩展请求 ID:空)在软件。 amazon.awssdk.core.internal.http.CombinedResponseHandler.handleErrorResponse(CombinedResponseHandler.java:123)~[sdk-core-2.17.46.jar:na]

Can you help me to solve this problem?你能帮我解决这个问题吗?

You're using the ARN of the topic, not the ARN of the subscription.您使用的是主题的 ARN,而不是订阅的 ARN。 Each subscription has an ARN too.每个订阅也有一个 ARN。 For example, if I create a topic named generic-topic the ARN for it looks like:例如,如果我创建一个名为generic-topic的主题,它的 ARN 如下所示:

arn:aws:sns:us-west-2:1234567890:generic-topic

Then, when I create a subscription (in this case an SQS queue) the subscription ARN looks like:然后,当我创建订阅(在本例中为 SQS 队列)时,订阅ARN 如下所示:

arn:aws:sns:us-west-2:1234567890:generic-topic:dc1580a7-1234-4b3d-12340-abcd12345678

If you're using the CLI you can see the subscriptions for a topic with something like:如果您使用的是 CLI,您可以看到一个主题的订阅,如下所示:

aws sns list-subscriptions-by-topic --topic-arn arn:aws:sns:us-west-2:1234567890:generic-topic

to get an output like:得到一个 output 这样的:

{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:us-west-2:1234567890:generic-topic:dc1580a7-1234-4b3d-12340-abcd12345678",
            "Owner": "1234567890",
            "Protocol": "sqs",
            "Endpoint": "arn:aws:sqs:us-west-2:1234567890:generic-queue",
            "TopicArn": "arn:aws:sns:us-west-2:1234567890:generic-topic"
        }
    ]
}

To solve my problem I just get the subcription Id like this:为了解决我的问题,我只是得到这样的订阅 ID:

public String retornaSubscriptionArnSNS(String arnSns)  {
    
    String resultado = "";
    
    try {
        ListSubscriptionsRequest request = ListSubscriptionsRequest.builder()
                .build();

        ListSubscriptionsResponse result = snsClient.listSubscriptions(request);
        
        int intSize = result.subscriptions().size(); 
        
        if (intSize > 0) {
            
            for (int i = 0; i < intSize-1; i++) {
                
                if (result.subscriptions().get(0).topicArn().equals(arnSns)) {
                    
                    resultado = result.subscriptions().get(i).subscriptionArn();
                    System.out.println("I found my topic: "+resultado);
                    break; 
                }
                
            }  

            
        } else {
            System.out.println("No one subscription found");
        }

        if (resultado == "") {
            System.out.println("Topic not found");
        }
        
        
    } catch (SnsException e) {

        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return resultado;
    
}

Then you can call this method like this:然后你可以像这样调用这个方法:

    retornaSubscriptionArnSNS("arn:aws:sns:us-east-1:9999999999:myTopic");

If the topic exists with subscription, then will print:如果主题存在订阅,则将打印:

I found my topic: : arn:aws:sns:us-east-1:9999999999:myTopic:3e32b1c0-d3b7-40a6-8000-99220be49445我找到了我的主题::arn:aws:sns:us-east-1:9999999999:myTopic:3e32b1c0-d3b7-40a6-8000-99220be49445

Else:别的:

No one subscription found找不到任何订阅

I hope that code can be useful for you!希望代码对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM