sendMessage
SQSClient.sendMessage(queueUrl, messageBody, options)
向指定的 Amazon Simple Queue Service (SQS) 队列发送消息。
参数
名称 | 类型 | 描述 |
---|
queueUrl | string | 要发送消息到的 Amazon SQS 队列的 URL。队列 URL 和名称区分大小写。 |
messageBody | string | 要发送的消息。最小大小为一个字符。最大大小为 256 KB。 |
options | SendMessageOptions (可选) | 请求的选项。 |
SendMessageOptions
名称 | 类型 | 描述 |
---|
messageDeduplicationId | string(可选) | 用于发送消息去重处理的令牌。此参数仅适用于 FIFO(先进先出)队列。如果使用特定 MessageDeduplicationId 发送的消息成功,则具有相同 MessageDeduplicationId 的任何消息都将被接受,但在 5 分钟的去重间隔内不会被传递。 |
messageGroupId | string(可选) | 指定消息属于特定消息组的标签。属于同一消息组的消息以 FIFO 方式处理。不同消息组中的消息可能乱序处理。 |
messageAttributes | object(可选) | 每个消息属性都包含一个 Name 、Type 和 Value 。有关更多信息,请参阅 Amazon SQS 消息属性。 |
delaySeconds | number(可选) | 延迟特定消息的时间长度,单位为秒。有效值:0 到 900。最大值:15 分钟。delaySeconds 值为正的消息在延迟期结束后可用于处理。如果您未指定值,则应用队列的默认值。 |
返回值
类型 | 描述 |
---|
Promise<object> | 一个 Promise,它将在发送消息后 fulfill,返回一个包含 id 字符串属性(消息的唯一标识符)和 bodyMD5 字符串属性(非 URL 编码消息正文字符串的 MD5 摘要)的对象。 |
示例
import exec from 'k6/execution';
import {
AWSConfig,
SQSClient,
} from 'https://jslib.k6.io/aws/0.13.0/sqs.js';
const awsConfig = new AWSConfig({
region: __ENV.AWS_REGION,
accessKeyId: __ENV.AWS_ACCESS_KEY_ID,
secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY,
sessionToken: __ENV.AWS_SESSION_TOKEN,
});
const sqs = new SQSClient(awsConfig);
const testQueue = 'https://sqs.us-east-1.amazonaws.com/000000000/test-queue';
export default async function () {
// If our test queue does not exist, abort the execution.
const queuesResponse = await sqs.listQueues();
if (queuesResponse.urls.filter((q) => q === testQueue).length == 0) {
exec.test.abort();
}
// Send message to test queue
await sqs.sendMessage(testQueue, 'test', {
messageAttributes: {
'test-string': {
type: 'String',
value: 'test',
},
'test-number': {
type: 'Number',
value: '23',
},
'test-binary': {
type: 'Binary',
value: 'dGVzdA==',
},
},
});
}