Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
SQSContoh Amazon menggunakan SDK untuk Ruby
Contoh kode berikut menunjukkan kepada Anda cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for Ruby With AmazonSQS.
Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.
Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.
Tindakan
Contoh kode berikut menunjukkan cara menggunakanChangeMessageVisibility
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Replace us-west-2 with the AWS Region you're using for Amazon SQS. sqs = Aws::SQS::Client.new(region: 'us-west-2') begin queue_name = 'my-queue' queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url # Receive up to 10 messages receive_message_result_before = sqs.receive_message({ queue_url: queue_url, max_number_of_messages: 10 }) puts "Before attempting to change message visibility timeout: received #{receive_message_result_before.messages.count} message(s)." receive_message_result_before.messages.each do |message| sqs.change_message_visibility({ queue_url: queue_url, receipt_handle: message.receipt_handle, visibility_timeout: 30 # This message will not be visible for 30 seconds after first receipt. }) end # Try to retrieve the original messages after setting their visibility timeout. receive_message_result_after = sqs.receive_message({ queue_url: queue_url, max_number_of_messages: 10 }) puts "\nAfter attempting to change message visibility timeout: received #{receive_message_result_after.messages.count} message(s)." rescue Aws::SQS::Errors::NonExistentQueue puts "Cannot receive messages for a queue named '#{queue_name}', as it does not exist." end
-
Untuk API detailnya, lihat ChangeMessageVisibilitydi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanCreateQueue
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. # This code example demonstrates how to create a queue in Amazon Simple Queue Service (Amazon SQS). require 'aws-sdk-sqs' # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_name [String] The name of the queue. # @return [Boolean] true if the queue was created; otherwise, false. # @example # exit 1 unless queue_created?( # Aws::SQS::Client.new(region: 'us-west-2'), # 'my-queue' # ) def queue_created?(sqs_client, queue_name) sqs_client.create_queue(queue_name: queue_name) true rescue StandardError => e puts "Error creating queue: #{e.message}" false end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = 'us-west-2' queue_name = 'my-queue' sqs_client = Aws::SQS::Client.new(region: region) puts "Creating the queue named '#{queue_name}'..." if queue_created?(sqs_client, queue_name) puts 'Queue created.' else puts 'Queue not created.' end end # Example usage: run_me if $PROGRAM_NAME == __FILE__
-
Untuk API detailnya, lihat CreateQueuedi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanDeleteQueue
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Replace us-west-2 with the AWS Region you're using for Amazon SQS. sqs = Aws::SQS::Client.new(region: 'us-west-2') sqs.delete_queue(queue_url: URL)
-
Untuk API detailnya, lihat DeleteQueuedi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanListQueues
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' require 'aws-sdk-sts' # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @example # list_queue_urls(Aws::SQS::Client.new(region: 'us-west-2')) def list_queue_urls(sqs_client) queues = sqs_client.list_queues queues.queue_urls.each do |url| puts url end rescue StandardError => e puts "Error listing queue URLs: #{e.message}" end # Lists the attributes of a queue in Amazon Simple Queue Service (Amazon SQS). # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @example # list_queue_attributes( # Aws::SQS::Client.new(region: 'us-west-2'), # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' # ) def list_queue_attributes(sqs_client, queue_url) attributes = sqs_client.get_queue_attributes( queue_url: queue_url, attribute_names: ['All'] ) attributes.attributes.each do |key, value| puts "#{key}: #{value}" end rescue StandardError => e puts "Error getting queue attributes: #{e.message}" end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = 'us-west-2' queue_name = 'my-queue' sqs_client = Aws::SQS::Client.new(region: region) puts 'Listing available queue URLs...' list_queue_urls(sqs_client) sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' queue_url = "https://sqs.#{region}.amazonaws.com/#{sts_client.get_caller_identity.account}/#{queue_name}" puts "\nGetting information about queue '#{queue_name}'..." list_queue_attributes(sqs_client, queue_url) end
-
Untuk API detailnya, lihat ListQueuesdi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanReceiveMessage
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' require 'aws-sdk-sts' # Receives messages in a queue in Amazon Simple Queue Service (Amazon SQS). # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param max_number_of_messages [Integer] The maximum number of messages # to receive. This number must be 10 or less. The default is 10. # @example # receive_messages( # Aws::SQS::Client.new(region: 'us-west-2'), # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue', # 10 # ) def receive_messages(sqs_client, queue_url, max_number_of_messages = 10) if max_number_of_messages > 10 puts 'Maximum number of messages to receive must be 10 or less. ' \ 'Stopping program.' return end response = sqs_client.receive_message( queue_url: queue_url, max_number_of_messages: max_number_of_messages ) if response.messages.count.zero? puts 'No messages to receive, or all messages have already ' \ 'been previously received.' return end response.messages.each do |message| puts '-' * 20 puts "Message body: #{message.body}" puts "Message ID: #{message.message_id}" end rescue StandardError => e puts "Error receiving messages: #{e.message}" end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = 'us-west-2' queue_name = 'my-queue' max_number_of_messages = 10 sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' queue_url = "https://sqs.#{region}.amazonaws.com/#{sts_client.get_caller_identity.account}/#{queue_name}" sqs_client = Aws::SQS::Client.new(region: region) puts "Receiving messages from queue '#{queue_name}'..." receive_messages(sqs_client, queue_url, max_number_of_messages) end # Example usage: run_me if $PROGRAM_NAME == __FILE__
-
Untuk API detailnya, lihat ReceiveMessagedi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanSendMessage
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' require 'aws-sdk-sts' # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param message_body [String] The contents of the message to be sent. # @return [Boolean] true if the message was sent; otherwise, false. # @example # exit 1 unless message_sent?( # Aws::SQS::Client.new(region: 'us-west-2'), # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue', # 'This is my message.' # ) def message_sent?(sqs_client, queue_url, message_body) sqs_client.send_message( queue_url: queue_url, message_body: message_body ) true rescue StandardError => e puts "Error sending message: #{e.message}" false end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = 'us-west-2' queue_name = 'my-queue' message_body = 'This is my message.' sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' queue_url = "https://sqs.#{region}.amazonaws.com/#{sts_client.get_caller_identity.account}/#{queue_name}" sqs_client = Aws::SQS::Client.new(region: region) puts "Sending a message to the queue named '#{queue_name}'..." if message_sent?(sqs_client, queue_url, message_body) puts 'Message sent.' else puts 'Message not sent.' end end # Example usage: run_me if $PROGRAM_NAME == __FILE__
-
Untuk API detailnya, lihat SendMessagedi AWS SDK for Ruby APIReferensi.
-
Contoh kode berikut menunjukkan cara menggunakanSendMessageBatch
.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS
. require 'aws-sdk-sqs' require 'aws-sdk-sts' # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param entries [Hash] The contents of the messages to be sent, # in the correct format. # @return [Boolean] true if the messages were sent; otherwise, false. # @example # exit 1 unless messages_sent?( # Aws::SQS::Client.new(region: 'us-west-2'), # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue', # [ # { # id: 'Message1', # message_body: 'This is the first message.' # }, # { # id: 'Message2', # message_body: 'This is the second message.' # } # ] # ) def messages_sent?(sqs_client, queue_url, entries) sqs_client.send_message_batch( queue_url: queue_url, entries: entries ) true rescue StandardError => e puts "Error sending messages: #{e.message}" false end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = 'us-west-2' queue_name = 'my-queue' entries = [ { id: 'Message1', message_body: 'This is the first message.' }, { id: 'Message2', message_body: 'This is the second message.' } ] sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue' queue_url = "https://sqs.#{region}.amazonaws.com/#{sts_client.get_caller_identity.account}/#{queue_name}" sqs_client = Aws::SQS::Client.new(region: region) puts "Sending messages to the queue named '#{queue_name}'..." if messages_sent?(sqs_client, queue_url, entries) puts 'Messages sent.' else puts 'Messages not sent.' end end
-
Untuk API detailnya, lihat SendMessageBatchdi AWS SDK for Ruby APIReferensi.
-
Contoh nirserver
Contoh kode berikut menunjukkan cara menerapkan fungsi Lambda yang menerima peristiwa yang dipicu oleh menerima pesan dari antrianSQS. Fungsi mengambil pesan dari parameter acara dan mencatat konten setiap pesan.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di repositori contoh Nirserver
. Mengkonsumsi SQS acara dengan Lambda menggunakan Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 def lambda_handler(event:, context:) event['Records'].each do |message| process_message(message) end puts "done" end def process_message(message) begin puts "Processed message #{message['body']}" # TODO: Do interesting work based on the new message rescue StandardError => err puts "An error occurred" raise err end end
Contoh kode berikut menunjukkan cara mengimplementasikan respons batch sebagian untuk fungsi Lambda yang menerima peristiwa dari antrianSQS. Fungsi melaporkan kegagalan item batch dalam respons, memberi sinyal ke Lambda untuk mencoba lagi pesan tersebut nanti.
- SDKuntuk Ruby
-
catatan
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di repositori contoh Nirserver
. Melaporkan kegagalan item SQS batch dengan Lambda menggunakan Ruby.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'json' def lambda_handler(event:, context:) if event batch_item_failures = [] sqs_batch_response = {} event["Records"].each do |record| begin # process message rescue StandardError => e batch_item_failures << {"itemIdentifier" => record['messageId']} end end sqs_batch_response["batchItemFailures"] = batch_item_failures return sqs_batch_response end end