SHARE
TWEET

Untitled

a guest Aug 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Creates an SNS topic and SQS queue. The queue is subscribed to the topic.
  2. static void CreateTopicandQueue(){
  3.         //create a new SNS topic
  4.         snsTopicName="AmazonTextractTopic" + Long.toString(System.currentTimeMillis());
  5.         CreateTopicRequest createTopicRequest = new CreateTopicRequest(snsTopicName);
  6.         CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest);
  7.         snsTopicArn=createTopicResult.getTopicArn();
  8.  
  9.         //Create a new SQS Queue
  10.         sqsQueueName="AmazonTextractQueue" + Long.toString(System.currentTimeMillis());
  11.         final CreateQueueRequest createQueueRequest = new CreateQueueRequest(sqsQueueName);
  12.         sqsQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
  13.         sqsQueueArn = sqs.getQueueAttributes(sqsQueueUrl, Arrays.asList("QueueArn")).getAttributes().get("QueueArn");
  14.  
  15.         //Subscribe SQS queue to SNS topic
  16.         String sqsSubscriptionArn = sns.subscribe(snsTopicArn, "sqs", sqsQueueArn).getSubscriptionArn();
  17.  
  18.         // Authorize queue
  19.         Policy policy = new Policy().withStatements(
  20.                 new Statement(Effect.Allow)
  21.                         .withPrincipals(Principal.AllUsers)
  22.                         .withActions(SQSActions.SendMessage)
  23.                         .withResources(new Resource(sqsQueueArn))
  24.                         .withConditions(new Condition().withType("ArnEquals").withConditionKey("aws:SourceArn").withValues(snsTopicArn))
  25.         );
  26.  
  27.  
  28.         Map queueAttributes = new HashMap();
  29.         queueAttributes.put(QueueAttributeName.Policy.toString(), policy.toJson());
  30.         sqs.setQueueAttributes(new SetQueueAttributesRequest(sqsQueueUrl, queueAttributes));
  31.  
  32.  
  33.         System.out.println("Topic arn: " + snsTopicArn);
  34.         System.out.println("Queue arn: " + sqsQueueArn);
  35.         System.out.println("Queue url: " + sqsQueueUrl);
  36.         System.out.println("Queue sub arn: " + sqsSubscriptionArn );
  37.     }
  38.      
  39. private static void StartDocumentTextDetection(String bucket, String document) throws Exception{
  40.  
  41.         //Create notification channel
  42.         NotificationChannel channel = new NotificationChannel()
  43.                 .withRoleArn(roleArn)
  44.                 .withSNSTopicArn(snsTopicArn);
  45.  
  46.  
  47.         S3Object s3ObjectTextract = new S3Object();
  48.         s3ObjectTextract.setBucket(bucket);
  49.         s3ObjectTextract.setName(document);
  50.  
  51.         StartDocumentTextDetectionRequest req = new StartDocumentTextDetectionRequest()
  52.                 .withDocumentLocation(new DocumentLocation()
  53.                         .withS3Object(s3ObjectTextract))
  54.                 .withJobTag("DetectingText")
  55.                 .withNotificationChannel(channel);
  56.  
  57.         System.out.println("Found the document: " + document);
  58.         System.out.println(req.toString());
  59.  
  60.         StartDocumentTextDetectionResult startDocumentTextDetectionResult = textract.startDocumentTextDetection(req);
  61.  
  62.         startJobId=startDocumentTextDetectionResult.getJobId();
  63.     }
  64.      
  65. {DocumentLocation: {S3Object: {Bucket: <bucketname>,Name: <filename>,}},JobTag: DetectingText,NotificationChannel: {SNSTopicArn: <Arn>,RoleArn: <arn}}
  66. Exception in thread "main" com.amazonaws.services.textract.model.InvalidParameterException: Request has invalid parameters (Service: AmazonTextract; Status Code: 400; Error Code: InvalidParameterException; Request ID: 8ff9fe9e-e880-440d-9b27-b4a4521bfb28)
  67.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1712)
  68.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1367)
  69.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1113)
  70.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:770)
  71.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:744)
  72.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726)
  73.     at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:686)
  74.     at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668)
  75.     at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532)
  76.     at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512)
  77.     at com.amazonaws.services.textract.AmazonTextractClient.doInvoke(AmazonTextractClient.java:816)
  78.     at com.amazonaws.services.textract.AmazonTextractClient.invoke(AmazonTextractClient.java:783)
  79.     at com.amazonaws.services.textract.AmazonTextractClient.invoke(AmazonTextractClient.java:772)
  80.     at com.amazonaws.services.textract.AmazonTextractClient.executeStartDocumentTextDetection(AmazonTextractClient.java:738)
  81.     at com.amazonaws.services.textract.AmazonTextractClient.startDocumentTextDetection(AmazonTextractClient.java:708)
  82.     at AnalyzeDocumentAsync.StartDocumentTextDetection(AnalyzeDocumentAsync.java:279)
  83.     at AnalyzeDocumentAsync.ProcessDocument(AnalyzeDocumentAsync.java:158)
  84.     at AnalyzeDocumentAsync.main(AnalyzeDocumentAsync.java:91)
  85.  
  86. Process finished with exit code 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top