将 JSON 文件上传到 Amazon SQS 时出错
Error while uploading a JSON file to Amazon SQS
我有以下代码,主要是尝试将 .json 文件写入 SQS
import json
import uuid
import time
import boto.sqs
import boto
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.message import RawMessage
sqs = boto.sqs.connect_to_region("ap-southeast-1")
queue = sqs.get_queue("Demon")
json_fileone=open('emp.json') ## this is only one JSON file
dataone=json.load(json_fileone)
print dataone
[queue.write(queue.new_message(i)) for i in dataone]
print "File sent successfully to queue"
这会上传所需的 emp.json 文件,我需要做的是 运行 对文件数量进行循环,因为我有 emp1.json、emp2.json、emp3.json,emp4.json 到 SQS,默认情况下 BOTO 在发送前进行 BASE64 编码...我需要以相同的格式发送这些文件,即 '.json'
根据您的问题和我们的意见,您需要使用glob。
import glob
json_files = glob.glob("*.json")
for json_file in json_files:
process_file(json_file)
因此,将您的进程移至函数 process_file
。同时使用with
语句处理文件。
def process_file(json_file):
sqs = boto.sqs.connect_to_region("ap-southeast-1")
queue = sqs.get_queue("Demon")
with open('emp.json') as json_fileone:
dataone=json.load(json_fileone)
.....
我有以下代码,主要是尝试将 .json 文件写入 SQS
import json
import uuid
import time
import boto.sqs
import boto
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
from boto.sqs.message import RawMessage
sqs = boto.sqs.connect_to_region("ap-southeast-1")
queue = sqs.get_queue("Demon")
json_fileone=open('emp.json') ## this is only one JSON file
dataone=json.load(json_fileone)
print dataone
[queue.write(queue.new_message(i)) for i in dataone]
print "File sent successfully to queue"
这会上传所需的 emp.json 文件,我需要做的是 运行 对文件数量进行循环,因为我有 emp1.json、emp2.json、emp3.json,emp4.json 到 SQS,默认情况下 BOTO 在发送前进行 BASE64 编码...我需要以相同的格式发送这些文件,即 '.json'
根据您的问题和我们的意见,您需要使用glob。
import glob
json_files = glob.glob("*.json")
for json_file in json_files:
process_file(json_file)
因此,将您的进程移至函数 process_file
。同时使用with
语句处理文件。
def process_file(json_file):
sqs = boto.sqs.connect_to_region("ap-southeast-1")
queue = sqs.get_queue("Demon")
with open('emp.json') as json_fileone:
dataone=json.load(json_fileone)
.....