我需要将几个.json文件发送到AWS SQS排队服务。有人能告诉我使用boto的确切代码会完成这个吗?
答案 0 :(得分:1)
这些方面应该有效: (你可以先检查语法,我还没有测试过这个)
import boto.sqs
import json
queue_name = 'YOUR-QUEUE'
sqs = boto.sqs.connect_to_region('us-east-1')
queue = sqs.get_queue(queue_name)
[queue.write(queue.new_message(json.load(i))) for i in ['file1.json', 'file2.json']]
答案 1 :(得分:0)
Boto的new_message()
调用期待一个字符串作为其参数。您的文件已经以字符串格式存储JSON。您不需要JSON模块。只是不要忘记剥离你的行结尾 - 在Windows上的\ r \ n(CRLF)和在Unix上的\ n(LF)
但是,如果你有一个JSON对象,那么这是一个不同的故事。使用json.dumps
获取JSON对象并将其作为字符串转储。
From the boto (not version 3) docs
At this point the variable conn will point to an SQSConnection object in the US-WEST-2 region. Bear in mind that just as any other AWS service, SQS is region-specific. In this example, the AWS access key and AWS secret key are passed in to the method explicitly. Alternatively, you can set the environment variables:
AWS_ACCESS_KEY_ID - Your AWS Access Key ID
AWS_SECRET_ACCESS_KEY - Your AWS Secret Access Key
我在python 2.7.12和boto(不是boto 3)上测试了它,并且它有效。
import boto.sqs
import sys
# either use environment variables for your access keys, or the flags here
conn = boto.sqs.connect_to_region("us-west-2", aws_access_key_id="the_key", aws_secret_access_key="the_secret_key")
q = conn.get_queue('the_queue_name')
if not q:
print "unable to locate queue! exiting..."
sys.exit()
for src_file in ['myfile1.json', 'myfile2.json']:
with open(src_file, 'r') as json_file:
for line in json_file:
print "writing %s" % line
q.write(q.new_message(line.rstrip('\r\n')))