繁体   English   中英

无法使用Python BOTO将具有相同语法的json文件上传到Amazon SQS

[英]Unable to upload a json file in same syntax to Amazon SQS using Python BOTO

我的json文件如下所示:

{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}

我正在使用以下Boto代码将其上传到Amazon SQS

import json
import uuid
import time
import boto.sqs
import boto
import glob
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.message import RawMessage


def process_file(json_file):
        sqs = boto.sqs.connect_to_region("ap-southeast-1")
        queue = sqs.get_queue("Killswitch")
        with open(json_file) as json_fileone:
                dataone=json.load(json_fileone)
                print dataone
                [queue.write(queue.new_message(i)) for i in dataone]
                print "File sent successfully to queue"

json_files = glob.glob("*json")
for json_file in json_files:
    process_file(json_file)

这段代码可以正常工作,但是当我通过浏览器上的AWS控制台检查我的SQS队列时,我看到4条消息,而不是由这个json文件组成的一条消息。

当我尝试通过boto代码读取相同内容时,最终收到以下内容:

id 
name 
price 
tags

我需要将整个文件上传到一个消息中,而不是4个对象/ 消息中

这是因为在这一行中:

[queue.write(queue.new_message(i)) for i in dataone]

for i in dataone意味着您正在迭代json对象的键

您正在有效地做

queue.write(queue.new_message('id')) 
queue.write(queue.new_message('name')) 
queue.write(queue.new_message('price')) 
queue.write(queue.new_message('tags')) 

显然,该API仅接受字符串,您可以尝试:

    with open(json_file) as json_fileone:
            dataone=json_fileone.read()
            queue.write(queue.new_message(dataone))
            print "File sent successfully to queue"

暂无
暂无

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

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